Bidirectional OneToMany relation causing maximum open cursors exceeded
I have 3 tablels Foo, Bar and Foo_x_Bar_Status, in my service im trying to create new record for Foo_x_Bar_Status. Im fetching managed object of Foo and managed object of Bar, then im creating new object of FooBarStatus and set previously fetched Foo and Bar objects on it. Then im trying to persist my newly created FooBarStatus wirh entityManager.merge()
(it needs to be merge as i need to return FooBarStatus managed object without searching for it again), however this merge fails with message: java.sql.SQLException: - ORA-01000: maximum open cursors exceeded
, how can i solve this issue?
P.S. i know i can just remove collections from Foo and Bar and access is via dedicated service, however it is essential for me to have this bidirectional relation
Foo:
@Entity(name = "Foo")
@Table(name = "FOO")
public class Foo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "FOO_SEQ")
private Long id;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST, mappedBy = "foo", orphanRemoval = true)
private Set<FooBarStatus> fooBarStatus;
}
Bar:
@Entity(name = "Bar")
@Table(name = "BAR")
public class BAR implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "BAR_SEQ")
private Long id;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST, mappedBy = "bar", orphanRemoval = true)
private Set<FooBarStatus> fooBarStatus;
}
FooBarStatus:
@Entity(name = "FooBarStatus")
@Table(name = "Foo_x_Bar_Status")
public class FooBarStatus implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "FOO_BAR_STATUS_SEQ")
private Long id;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "FOO_ID", nullable = false)
private Foo foo;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "BAR_ID", nullable = false)
private Bar bar;
Service
@Service
public class FooBarStatusServiceImpl implements FooBarStatus {
@Autowired
private FooRepository fooRepository;
@Autowired
private BarRepository barRepository;
@Autowired
private FooBarStatusReposiotry fooBarStatusReposiotry;
@Override
public FooBarStatus createFooBarStatus(Foo foo, Bar bar) {
//make foo nd bar managed
foo = fooRepository.createFoo(foo);
bar = barRepository.createBar(bar);
FooBarStatus fooBarStatus = new FooBarStatus();
fooBarStatus.setFoo(foo);
fooBarStatus.setBar(bar);
return fooBarStatusReposiotry.createFooBarStatus(fooBarStatus);
}
}
FooBarStatusReposioty:
@Repository
public class FooBarStatusReposiotyImpl implements FooBarStatusReposioty {
@PersistenceContext
public EntityManager entityManager;
@Override
public FooBarStatus createFooBarStatus(FooarStatus fooBarStatus){
return entityManager.merge(fooBarStatus);
}
}
Stacktrace:
2018-11-28 16:43:02 ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path threw exception [Request processing failed; nested exception is org.hibernate.exception.GenericJDBCException: could not extract ResultSet] with root cause
java.sql.SQLException: ORA-01000: maximum open cursors exceeded
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1059)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:522)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:257)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:587)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:225)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:53)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:774)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:925)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1111)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:4798)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:4845)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1501)
at sun.reflect.GeneratedMethodAccessor154.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114)
at com.sun.proxy.$Proxy119.executeQuery(Unknown Source)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.getResultSet(AbstractLoadPlanBasedLoader.java:434)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeQueryStatement(AbstractLoadPlanBasedLoader.java:186)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:121)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)
at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:88)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:688)
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:75)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1991)
at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:570)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:252)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:566)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:135)
at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:430)
at com.iwork.server.defautmodel.User.hashCode(User.java:20)
at com.iwork.server.defautmodel.UserNotification.hashCode(UserNotification.java:11)
at java.util.HashMap.hash(HashMap.java:339)
at java.util.HashMap.put(HashMap.java:612)
at java.util.HashSet.add(HashSet.java:220)
at java.util.AbstractCollection.addAll(AbstractCollection.java:344)
at org.hibernate.collection.internal.PersistentSet.endRead(PersistentSet.java:327)
at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:234)
at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:221)
at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:194)
at org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl.endLoading(CollectionReferenceInitializerImpl.java:154)
at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishLoadingCollections(AbstractRowReader.java:249)
at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:212)
at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:133)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:122)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)
at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:88)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:688)
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:75)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1991)
at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:570)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:252)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:566)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:135)
at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:430)
at com.application.server.defautmodel.Bar.hashCode(Bar.java:20)
at com.application.server.defautmodel.Foo.hashCode(Foo.java:11)
at java.util.HashMap.hash(HashMap.java:339)
at java.util.HashMap.put(HashMap.java:612)
at java.util.HashSet.add(HashSet.java:220)
at java.util.AbstractCollection.addAll(AbstractCollection.java:344)
the stacktrace is much longer but it seems like its looping.
Foo.java:11 and Bar.java:20 are pointing to @Data
annotation from Lombok
java hibernate jpa spring-data-jpa
|
show 5 more comments
I have 3 tablels Foo, Bar and Foo_x_Bar_Status, in my service im trying to create new record for Foo_x_Bar_Status. Im fetching managed object of Foo and managed object of Bar, then im creating new object of FooBarStatus and set previously fetched Foo and Bar objects on it. Then im trying to persist my newly created FooBarStatus wirh entityManager.merge()
(it needs to be merge as i need to return FooBarStatus managed object without searching for it again), however this merge fails with message: java.sql.SQLException: - ORA-01000: maximum open cursors exceeded
, how can i solve this issue?
P.S. i know i can just remove collections from Foo and Bar and access is via dedicated service, however it is essential for me to have this bidirectional relation
Foo:
@Entity(name = "Foo")
@Table(name = "FOO")
public class Foo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "FOO_SEQ")
private Long id;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST, mappedBy = "foo", orphanRemoval = true)
private Set<FooBarStatus> fooBarStatus;
}
Bar:
@Entity(name = "Bar")
@Table(name = "BAR")
public class BAR implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "BAR_SEQ")
private Long id;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST, mappedBy = "bar", orphanRemoval = true)
private Set<FooBarStatus> fooBarStatus;
}
FooBarStatus:
@Entity(name = "FooBarStatus")
@Table(name = "Foo_x_Bar_Status")
public class FooBarStatus implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "FOO_BAR_STATUS_SEQ")
private Long id;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "FOO_ID", nullable = false)
private Foo foo;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "BAR_ID", nullable = false)
private Bar bar;
Service
@Service
public class FooBarStatusServiceImpl implements FooBarStatus {
@Autowired
private FooRepository fooRepository;
@Autowired
private BarRepository barRepository;
@Autowired
private FooBarStatusReposiotry fooBarStatusReposiotry;
@Override
public FooBarStatus createFooBarStatus(Foo foo, Bar bar) {
//make foo nd bar managed
foo = fooRepository.createFoo(foo);
bar = barRepository.createBar(bar);
FooBarStatus fooBarStatus = new FooBarStatus();
fooBarStatus.setFoo(foo);
fooBarStatus.setBar(bar);
return fooBarStatusReposiotry.createFooBarStatus(fooBarStatus);
}
}
FooBarStatusReposioty:
@Repository
public class FooBarStatusReposiotyImpl implements FooBarStatusReposioty {
@PersistenceContext
public EntityManager entityManager;
@Override
public FooBarStatus createFooBarStatus(FooarStatus fooBarStatus){
return entityManager.merge(fooBarStatus);
}
}
Stacktrace:
2018-11-28 16:43:02 ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path threw exception [Request processing failed; nested exception is org.hibernate.exception.GenericJDBCException: could not extract ResultSet] with root cause
java.sql.SQLException: ORA-01000: maximum open cursors exceeded
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1059)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:522)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:257)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:587)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:225)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:53)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:774)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:925)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1111)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:4798)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:4845)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1501)
at sun.reflect.GeneratedMethodAccessor154.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114)
at com.sun.proxy.$Proxy119.executeQuery(Unknown Source)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.getResultSet(AbstractLoadPlanBasedLoader.java:434)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeQueryStatement(AbstractLoadPlanBasedLoader.java:186)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:121)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)
at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:88)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:688)
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:75)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1991)
at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:570)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:252)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:566)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:135)
at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:430)
at com.iwork.server.defautmodel.User.hashCode(User.java:20)
at com.iwork.server.defautmodel.UserNotification.hashCode(UserNotification.java:11)
at java.util.HashMap.hash(HashMap.java:339)
at java.util.HashMap.put(HashMap.java:612)
at java.util.HashSet.add(HashSet.java:220)
at java.util.AbstractCollection.addAll(AbstractCollection.java:344)
at org.hibernate.collection.internal.PersistentSet.endRead(PersistentSet.java:327)
at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:234)
at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:221)
at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:194)
at org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl.endLoading(CollectionReferenceInitializerImpl.java:154)
at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishLoadingCollections(AbstractRowReader.java:249)
at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:212)
at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:133)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:122)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)
at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:88)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:688)
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:75)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1991)
at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:570)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:252)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:566)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:135)
at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:430)
at com.application.server.defautmodel.Bar.hashCode(Bar.java:20)
at com.application.server.defautmodel.Foo.hashCode(Foo.java:11)
at java.util.HashMap.hash(HashMap.java:339)
at java.util.HashMap.put(HashMap.java:612)
at java.util.HashSet.add(HashSet.java:220)
at java.util.AbstractCollection.addAll(AbstractCollection.java:344)
the stacktrace is much longer but it seems like its looping.
Foo.java:11 and Bar.java:20 are pointing to @Data
annotation from Lombok
java hibernate jpa spring-data-jpa
Firstly, every entity must have an ID and FooBarStatus doesn't. Secondly, use entityManager.persist() which "Make an instance managed and persistent." so you don't have to query again. Fix these basic issues and see where you're at.
– Alan Hay
Nov 28 '18 at 17:38
@AlanHay Foo and Bar are simplified versions of my classes just to represent the issue, of course they do have Id's. Please provide some context why persist should work differently from merge in this case? it seems like a blind shoot
– Akka Jaworek
Nov 28 '18 at 17:58
Sorry I can't guess what is wrong. Why don't you post all the relevant code and use the API properly as a first step.
– Alan Hay
Nov 28 '18 at 18:08
@AlanHay added full code with repository, changed merge to persist, it did not solve the problem.
– Akka Jaworek
Nov 28 '18 at 18:38
2
Looks like you are doing something funky with your hashcode methods, causing a whole bunch of queries to get triggered. You seem to do this on Bar, foo AND UserNotification and User classes. Don't access lazy or any relationship in a hashcode or equality check, especially if you are using maps or sets which use them.
– Chris
Nov 28 '18 at 19:43
|
show 5 more comments
I have 3 tablels Foo, Bar and Foo_x_Bar_Status, in my service im trying to create new record for Foo_x_Bar_Status. Im fetching managed object of Foo and managed object of Bar, then im creating new object of FooBarStatus and set previously fetched Foo and Bar objects on it. Then im trying to persist my newly created FooBarStatus wirh entityManager.merge()
(it needs to be merge as i need to return FooBarStatus managed object without searching for it again), however this merge fails with message: java.sql.SQLException: - ORA-01000: maximum open cursors exceeded
, how can i solve this issue?
P.S. i know i can just remove collections from Foo and Bar and access is via dedicated service, however it is essential for me to have this bidirectional relation
Foo:
@Entity(name = "Foo")
@Table(name = "FOO")
public class Foo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "FOO_SEQ")
private Long id;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST, mappedBy = "foo", orphanRemoval = true)
private Set<FooBarStatus> fooBarStatus;
}
Bar:
@Entity(name = "Bar")
@Table(name = "BAR")
public class BAR implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "BAR_SEQ")
private Long id;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST, mappedBy = "bar", orphanRemoval = true)
private Set<FooBarStatus> fooBarStatus;
}
FooBarStatus:
@Entity(name = "FooBarStatus")
@Table(name = "Foo_x_Bar_Status")
public class FooBarStatus implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "FOO_BAR_STATUS_SEQ")
private Long id;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "FOO_ID", nullable = false)
private Foo foo;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "BAR_ID", nullable = false)
private Bar bar;
Service
@Service
public class FooBarStatusServiceImpl implements FooBarStatus {
@Autowired
private FooRepository fooRepository;
@Autowired
private BarRepository barRepository;
@Autowired
private FooBarStatusReposiotry fooBarStatusReposiotry;
@Override
public FooBarStatus createFooBarStatus(Foo foo, Bar bar) {
//make foo nd bar managed
foo = fooRepository.createFoo(foo);
bar = barRepository.createBar(bar);
FooBarStatus fooBarStatus = new FooBarStatus();
fooBarStatus.setFoo(foo);
fooBarStatus.setBar(bar);
return fooBarStatusReposiotry.createFooBarStatus(fooBarStatus);
}
}
FooBarStatusReposioty:
@Repository
public class FooBarStatusReposiotyImpl implements FooBarStatusReposioty {
@PersistenceContext
public EntityManager entityManager;
@Override
public FooBarStatus createFooBarStatus(FooarStatus fooBarStatus){
return entityManager.merge(fooBarStatus);
}
}
Stacktrace:
2018-11-28 16:43:02 ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path threw exception [Request processing failed; nested exception is org.hibernate.exception.GenericJDBCException: could not extract ResultSet] with root cause
java.sql.SQLException: ORA-01000: maximum open cursors exceeded
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1059)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:522)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:257)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:587)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:225)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:53)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:774)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:925)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1111)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:4798)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:4845)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1501)
at sun.reflect.GeneratedMethodAccessor154.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114)
at com.sun.proxy.$Proxy119.executeQuery(Unknown Source)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.getResultSet(AbstractLoadPlanBasedLoader.java:434)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeQueryStatement(AbstractLoadPlanBasedLoader.java:186)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:121)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)
at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:88)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:688)
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:75)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1991)
at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:570)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:252)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:566)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:135)
at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:430)
at com.iwork.server.defautmodel.User.hashCode(User.java:20)
at com.iwork.server.defautmodel.UserNotification.hashCode(UserNotification.java:11)
at java.util.HashMap.hash(HashMap.java:339)
at java.util.HashMap.put(HashMap.java:612)
at java.util.HashSet.add(HashSet.java:220)
at java.util.AbstractCollection.addAll(AbstractCollection.java:344)
at org.hibernate.collection.internal.PersistentSet.endRead(PersistentSet.java:327)
at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:234)
at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:221)
at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:194)
at org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl.endLoading(CollectionReferenceInitializerImpl.java:154)
at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishLoadingCollections(AbstractRowReader.java:249)
at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:212)
at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:133)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:122)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)
at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:88)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:688)
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:75)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1991)
at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:570)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:252)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:566)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:135)
at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:430)
at com.application.server.defautmodel.Bar.hashCode(Bar.java:20)
at com.application.server.defautmodel.Foo.hashCode(Foo.java:11)
at java.util.HashMap.hash(HashMap.java:339)
at java.util.HashMap.put(HashMap.java:612)
at java.util.HashSet.add(HashSet.java:220)
at java.util.AbstractCollection.addAll(AbstractCollection.java:344)
the stacktrace is much longer but it seems like its looping.
Foo.java:11 and Bar.java:20 are pointing to @Data
annotation from Lombok
java hibernate jpa spring-data-jpa
I have 3 tablels Foo, Bar and Foo_x_Bar_Status, in my service im trying to create new record for Foo_x_Bar_Status. Im fetching managed object of Foo and managed object of Bar, then im creating new object of FooBarStatus and set previously fetched Foo and Bar objects on it. Then im trying to persist my newly created FooBarStatus wirh entityManager.merge()
(it needs to be merge as i need to return FooBarStatus managed object without searching for it again), however this merge fails with message: java.sql.SQLException: - ORA-01000: maximum open cursors exceeded
, how can i solve this issue?
P.S. i know i can just remove collections from Foo and Bar and access is via dedicated service, however it is essential for me to have this bidirectional relation
Foo:
@Entity(name = "Foo")
@Table(name = "FOO")
public class Foo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "FOO_SEQ")
private Long id;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST, mappedBy = "foo", orphanRemoval = true)
private Set<FooBarStatus> fooBarStatus;
}
Bar:
@Entity(name = "Bar")
@Table(name = "BAR")
public class BAR implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "BAR_SEQ")
private Long id;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST, mappedBy = "bar", orphanRemoval = true)
private Set<FooBarStatus> fooBarStatus;
}
FooBarStatus:
@Entity(name = "FooBarStatus")
@Table(name = "Foo_x_Bar_Status")
public class FooBarStatus implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "FOO_BAR_STATUS_SEQ")
private Long id;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "FOO_ID", nullable = false)
private Foo foo;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "BAR_ID", nullable = false)
private Bar bar;
Service
@Service
public class FooBarStatusServiceImpl implements FooBarStatus {
@Autowired
private FooRepository fooRepository;
@Autowired
private BarRepository barRepository;
@Autowired
private FooBarStatusReposiotry fooBarStatusReposiotry;
@Override
public FooBarStatus createFooBarStatus(Foo foo, Bar bar) {
//make foo nd bar managed
foo = fooRepository.createFoo(foo);
bar = barRepository.createBar(bar);
FooBarStatus fooBarStatus = new FooBarStatus();
fooBarStatus.setFoo(foo);
fooBarStatus.setBar(bar);
return fooBarStatusReposiotry.createFooBarStatus(fooBarStatus);
}
}
FooBarStatusReposioty:
@Repository
public class FooBarStatusReposiotyImpl implements FooBarStatusReposioty {
@PersistenceContext
public EntityManager entityManager;
@Override
public FooBarStatus createFooBarStatus(FooarStatus fooBarStatus){
return entityManager.merge(fooBarStatus);
}
}
Stacktrace:
2018-11-28 16:43:02 ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path threw exception [Request processing failed; nested exception is org.hibernate.exception.GenericJDBCException: could not extract ResultSet] with root cause
java.sql.SQLException: ORA-01000: maximum open cursors exceeded
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1059)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:522)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:257)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:587)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:225)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:53)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:774)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:925)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1111)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:4798)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:4845)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1501)
at sun.reflect.GeneratedMethodAccessor154.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114)
at com.sun.proxy.$Proxy119.executeQuery(Unknown Source)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.getResultSet(AbstractLoadPlanBasedLoader.java:434)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeQueryStatement(AbstractLoadPlanBasedLoader.java:186)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:121)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)
at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:88)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:688)
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:75)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1991)
at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:570)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:252)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:566)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:135)
at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:430)
at com.iwork.server.defautmodel.User.hashCode(User.java:20)
at com.iwork.server.defautmodel.UserNotification.hashCode(UserNotification.java:11)
at java.util.HashMap.hash(HashMap.java:339)
at java.util.HashMap.put(HashMap.java:612)
at java.util.HashSet.add(HashSet.java:220)
at java.util.AbstractCollection.addAll(AbstractCollection.java:344)
at org.hibernate.collection.internal.PersistentSet.endRead(PersistentSet.java:327)
at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:234)
at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:221)
at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:194)
at org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl.endLoading(CollectionReferenceInitializerImpl.java:154)
at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishLoadingCollections(AbstractRowReader.java:249)
at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:212)
at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:133)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:122)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)
at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:88)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:688)
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:75)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1991)
at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:570)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:252)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:566)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:135)
at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:430)
at com.application.server.defautmodel.Bar.hashCode(Bar.java:20)
at com.application.server.defautmodel.Foo.hashCode(Foo.java:11)
at java.util.HashMap.hash(HashMap.java:339)
at java.util.HashMap.put(HashMap.java:612)
at java.util.HashSet.add(HashSet.java:220)
at java.util.AbstractCollection.addAll(AbstractCollection.java:344)
the stacktrace is much longer but it seems like its looping.
Foo.java:11 and Bar.java:20 are pointing to @Data
annotation from Lombok
java hibernate jpa spring-data-jpa
java hibernate jpa spring-data-jpa
edited Nov 28 '18 at 19:09
Akka Jaworek
asked Nov 28 '18 at 15:58
Akka JaworekAkka Jaworek
4872520
4872520
Firstly, every entity must have an ID and FooBarStatus doesn't. Secondly, use entityManager.persist() which "Make an instance managed and persistent." so you don't have to query again. Fix these basic issues and see where you're at.
– Alan Hay
Nov 28 '18 at 17:38
@AlanHay Foo and Bar are simplified versions of my classes just to represent the issue, of course they do have Id's. Please provide some context why persist should work differently from merge in this case? it seems like a blind shoot
– Akka Jaworek
Nov 28 '18 at 17:58
Sorry I can't guess what is wrong. Why don't you post all the relevant code and use the API properly as a first step.
– Alan Hay
Nov 28 '18 at 18:08
@AlanHay added full code with repository, changed merge to persist, it did not solve the problem.
– Akka Jaworek
Nov 28 '18 at 18:38
2
Looks like you are doing something funky with your hashcode methods, causing a whole bunch of queries to get triggered. You seem to do this on Bar, foo AND UserNotification and User classes. Don't access lazy or any relationship in a hashcode or equality check, especially if you are using maps or sets which use them.
– Chris
Nov 28 '18 at 19:43
|
show 5 more comments
Firstly, every entity must have an ID and FooBarStatus doesn't. Secondly, use entityManager.persist() which "Make an instance managed and persistent." so you don't have to query again. Fix these basic issues and see where you're at.
– Alan Hay
Nov 28 '18 at 17:38
@AlanHay Foo and Bar are simplified versions of my classes just to represent the issue, of course they do have Id's. Please provide some context why persist should work differently from merge in this case? it seems like a blind shoot
– Akka Jaworek
Nov 28 '18 at 17:58
Sorry I can't guess what is wrong. Why don't you post all the relevant code and use the API properly as a first step.
– Alan Hay
Nov 28 '18 at 18:08
@AlanHay added full code with repository, changed merge to persist, it did not solve the problem.
– Akka Jaworek
Nov 28 '18 at 18:38
2
Looks like you are doing something funky with your hashcode methods, causing a whole bunch of queries to get triggered. You seem to do this on Bar, foo AND UserNotification and User classes. Don't access lazy or any relationship in a hashcode or equality check, especially if you are using maps or sets which use them.
– Chris
Nov 28 '18 at 19:43
Firstly, every entity must have an ID and FooBarStatus doesn't. Secondly, use entityManager.persist() which "Make an instance managed and persistent." so you don't have to query again. Fix these basic issues and see where you're at.
– Alan Hay
Nov 28 '18 at 17:38
Firstly, every entity must have an ID and FooBarStatus doesn't. Secondly, use entityManager.persist() which "Make an instance managed and persistent." so you don't have to query again. Fix these basic issues and see where you're at.
– Alan Hay
Nov 28 '18 at 17:38
@AlanHay Foo and Bar are simplified versions of my classes just to represent the issue, of course they do have Id's. Please provide some context why persist should work differently from merge in this case? it seems like a blind shoot
– Akka Jaworek
Nov 28 '18 at 17:58
@AlanHay Foo and Bar are simplified versions of my classes just to represent the issue, of course they do have Id's. Please provide some context why persist should work differently from merge in this case? it seems like a blind shoot
– Akka Jaworek
Nov 28 '18 at 17:58
Sorry I can't guess what is wrong. Why don't you post all the relevant code and use the API properly as a first step.
– Alan Hay
Nov 28 '18 at 18:08
Sorry I can't guess what is wrong. Why don't you post all the relevant code and use the API properly as a first step.
– Alan Hay
Nov 28 '18 at 18:08
@AlanHay added full code with repository, changed merge to persist, it did not solve the problem.
– Akka Jaworek
Nov 28 '18 at 18:38
@AlanHay added full code with repository, changed merge to persist, it did not solve the problem.
– Akka Jaworek
Nov 28 '18 at 18:38
2
2
Looks like you are doing something funky with your hashcode methods, causing a whole bunch of queries to get triggered. You seem to do this on Bar, foo AND UserNotification and User classes. Don't access lazy or any relationship in a hashcode or equality check, especially if you are using maps or sets which use them.
– Chris
Nov 28 '18 at 19:43
Looks like you are doing something funky with your hashcode methods, causing a whole bunch of queries to get triggered. You seem to do this on Bar, foo AND UserNotification and User classes. Don't access lazy or any relationship in a hashcode or equality check, especially if you are using maps or sets which use them.
– Chris
Nov 28 '18 at 19:43
|
show 5 more comments
2 Answers
2
active
oldest
votes
This error is caused by Connections, Statements, and ResultSets left open. Example,
Connection with = getConnection();
PreparedStatement ps = con.prepareStatement
ResultSet rs = ps.executeQuery ();
So, you just close the
rs.close ();
ps.close ();
con.close ();
option is to use a try {} catch (e) {} finally {} and close in the finally
1
In the case of hibernate you have to close the Session
– Alan Jhone
Nov 28 '18 at 16:17
Do you get Entity Manager via dependency injection, like EJB and @PersistenceContext annotation or you use EntityManagerFactory? If you use annotation it should not happen.
– Kambiz
Nov 28 '18 at 16:32
@Kambiz i use@PersistenceContext
for entity manager
– Akka Jaworek
Nov 28 '18 at 17:23
@AlanJhone how to do so? i thought it was handled by@Transactional
annotation
– Akka Jaworek
Nov 28 '18 at 17:24
@AkkaJaworek I'm using org.hibernate.Session, try doing this in your persistence class: public void saveOuUpdate (Object obj) { Session session = getSession (); session.saveOrUpdate (obj); session.flush (); session.clear (); }
– Alan Jhone
Nov 28 '18 at 18:10
add a comment |
As suggested by @Chris in comment, root cause was that usage of @Data
annotation generated a HashCode calculation method and since Foo, Bar and FooBarStatus had lazy initialized attributes with bidirectional relation, hash calculation attempted to fetch data and lopped back infinitely. Since all Foo, Bar and FooBarStatus are unique by id's it is not necerssary to include joined attribtes in hash calculation. Therefore as per https://projectlombok.org/features/EqualsAndHashCode i have added @EqualsAndHashCode.Exclude to bidirectional lazy initialized attributes which excluded the from hash calculation. Example below:
@Entity(name = "Foo")
@Table(name = "FOO")
public class Foo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "FOO_SEQ")
private Long id;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST, mappedBy = "foo", orphanRemoval = true)
@EqualsAndHashCode.Exclude
private Set<FooBarStatus> fooBarStatus;
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53523452%2fbidirectional-onetomany-relation-causing-maximum-open-cursors-exceeded%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This error is caused by Connections, Statements, and ResultSets left open. Example,
Connection with = getConnection();
PreparedStatement ps = con.prepareStatement
ResultSet rs = ps.executeQuery ();
So, you just close the
rs.close ();
ps.close ();
con.close ();
option is to use a try {} catch (e) {} finally {} and close in the finally
1
In the case of hibernate you have to close the Session
– Alan Jhone
Nov 28 '18 at 16:17
Do you get Entity Manager via dependency injection, like EJB and @PersistenceContext annotation or you use EntityManagerFactory? If you use annotation it should not happen.
– Kambiz
Nov 28 '18 at 16:32
@Kambiz i use@PersistenceContext
for entity manager
– Akka Jaworek
Nov 28 '18 at 17:23
@AlanJhone how to do so? i thought it was handled by@Transactional
annotation
– Akka Jaworek
Nov 28 '18 at 17:24
@AkkaJaworek I'm using org.hibernate.Session, try doing this in your persistence class: public void saveOuUpdate (Object obj) { Session session = getSession (); session.saveOrUpdate (obj); session.flush (); session.clear (); }
– Alan Jhone
Nov 28 '18 at 18:10
add a comment |
This error is caused by Connections, Statements, and ResultSets left open. Example,
Connection with = getConnection();
PreparedStatement ps = con.prepareStatement
ResultSet rs = ps.executeQuery ();
So, you just close the
rs.close ();
ps.close ();
con.close ();
option is to use a try {} catch (e) {} finally {} and close in the finally
1
In the case of hibernate you have to close the Session
– Alan Jhone
Nov 28 '18 at 16:17
Do you get Entity Manager via dependency injection, like EJB and @PersistenceContext annotation or you use EntityManagerFactory? If you use annotation it should not happen.
– Kambiz
Nov 28 '18 at 16:32
@Kambiz i use@PersistenceContext
for entity manager
– Akka Jaworek
Nov 28 '18 at 17:23
@AlanJhone how to do so? i thought it was handled by@Transactional
annotation
– Akka Jaworek
Nov 28 '18 at 17:24
@AkkaJaworek I'm using org.hibernate.Session, try doing this in your persistence class: public void saveOuUpdate (Object obj) { Session session = getSession (); session.saveOrUpdate (obj); session.flush (); session.clear (); }
– Alan Jhone
Nov 28 '18 at 18:10
add a comment |
This error is caused by Connections, Statements, and ResultSets left open. Example,
Connection with = getConnection();
PreparedStatement ps = con.prepareStatement
ResultSet rs = ps.executeQuery ();
So, you just close the
rs.close ();
ps.close ();
con.close ();
option is to use a try {} catch (e) {} finally {} and close in the finally
This error is caused by Connections, Statements, and ResultSets left open. Example,
Connection with = getConnection();
PreparedStatement ps = con.prepareStatement
ResultSet rs = ps.executeQuery ();
So, you just close the
rs.close ();
ps.close ();
con.close ();
option is to use a try {} catch (e) {} finally {} and close in the finally
answered Nov 28 '18 at 16:15
Alan JhoneAlan Jhone
1
1
1
In the case of hibernate you have to close the Session
– Alan Jhone
Nov 28 '18 at 16:17
Do you get Entity Manager via dependency injection, like EJB and @PersistenceContext annotation or you use EntityManagerFactory? If you use annotation it should not happen.
– Kambiz
Nov 28 '18 at 16:32
@Kambiz i use@PersistenceContext
for entity manager
– Akka Jaworek
Nov 28 '18 at 17:23
@AlanJhone how to do so? i thought it was handled by@Transactional
annotation
– Akka Jaworek
Nov 28 '18 at 17:24
@AkkaJaworek I'm using org.hibernate.Session, try doing this in your persistence class: public void saveOuUpdate (Object obj) { Session session = getSession (); session.saveOrUpdate (obj); session.flush (); session.clear (); }
– Alan Jhone
Nov 28 '18 at 18:10
add a comment |
1
In the case of hibernate you have to close the Session
– Alan Jhone
Nov 28 '18 at 16:17
Do you get Entity Manager via dependency injection, like EJB and @PersistenceContext annotation or you use EntityManagerFactory? If you use annotation it should not happen.
– Kambiz
Nov 28 '18 at 16:32
@Kambiz i use@PersistenceContext
for entity manager
– Akka Jaworek
Nov 28 '18 at 17:23
@AlanJhone how to do so? i thought it was handled by@Transactional
annotation
– Akka Jaworek
Nov 28 '18 at 17:24
@AkkaJaworek I'm using org.hibernate.Session, try doing this in your persistence class: public void saveOuUpdate (Object obj) { Session session = getSession (); session.saveOrUpdate (obj); session.flush (); session.clear (); }
– Alan Jhone
Nov 28 '18 at 18:10
1
1
In the case of hibernate you have to close the Session
– Alan Jhone
Nov 28 '18 at 16:17
In the case of hibernate you have to close the Session
– Alan Jhone
Nov 28 '18 at 16:17
Do you get Entity Manager via dependency injection, like EJB and @PersistenceContext annotation or you use EntityManagerFactory? If you use annotation it should not happen.
– Kambiz
Nov 28 '18 at 16:32
Do you get Entity Manager via dependency injection, like EJB and @PersistenceContext annotation or you use EntityManagerFactory? If you use annotation it should not happen.
– Kambiz
Nov 28 '18 at 16:32
@Kambiz i use
@PersistenceContext
for entity manager– Akka Jaworek
Nov 28 '18 at 17:23
@Kambiz i use
@PersistenceContext
for entity manager– Akka Jaworek
Nov 28 '18 at 17:23
@AlanJhone how to do so? i thought it was handled by
@Transactional
annotation– Akka Jaworek
Nov 28 '18 at 17:24
@AlanJhone how to do so? i thought it was handled by
@Transactional
annotation– Akka Jaworek
Nov 28 '18 at 17:24
@AkkaJaworek I'm using org.hibernate.Session, try doing this in your persistence class: public void saveOuUpdate (Object obj) { Session session = getSession (); session.saveOrUpdate (obj); session.flush (); session.clear (); }
– Alan Jhone
Nov 28 '18 at 18:10
@AkkaJaworek I'm using org.hibernate.Session, try doing this in your persistence class: public void saveOuUpdate (Object obj) { Session session = getSession (); session.saveOrUpdate (obj); session.flush (); session.clear (); }
– Alan Jhone
Nov 28 '18 at 18:10
add a comment |
As suggested by @Chris in comment, root cause was that usage of @Data
annotation generated a HashCode calculation method and since Foo, Bar and FooBarStatus had lazy initialized attributes with bidirectional relation, hash calculation attempted to fetch data and lopped back infinitely. Since all Foo, Bar and FooBarStatus are unique by id's it is not necerssary to include joined attribtes in hash calculation. Therefore as per https://projectlombok.org/features/EqualsAndHashCode i have added @EqualsAndHashCode.Exclude to bidirectional lazy initialized attributes which excluded the from hash calculation. Example below:
@Entity(name = "Foo")
@Table(name = "FOO")
public class Foo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "FOO_SEQ")
private Long id;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST, mappedBy = "foo", orphanRemoval = true)
@EqualsAndHashCode.Exclude
private Set<FooBarStatus> fooBarStatus;
}
add a comment |
As suggested by @Chris in comment, root cause was that usage of @Data
annotation generated a HashCode calculation method and since Foo, Bar and FooBarStatus had lazy initialized attributes with bidirectional relation, hash calculation attempted to fetch data and lopped back infinitely. Since all Foo, Bar and FooBarStatus are unique by id's it is not necerssary to include joined attribtes in hash calculation. Therefore as per https://projectlombok.org/features/EqualsAndHashCode i have added @EqualsAndHashCode.Exclude to bidirectional lazy initialized attributes which excluded the from hash calculation. Example below:
@Entity(name = "Foo")
@Table(name = "FOO")
public class Foo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "FOO_SEQ")
private Long id;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST, mappedBy = "foo", orphanRemoval = true)
@EqualsAndHashCode.Exclude
private Set<FooBarStatus> fooBarStatus;
}
add a comment |
As suggested by @Chris in comment, root cause was that usage of @Data
annotation generated a HashCode calculation method and since Foo, Bar and FooBarStatus had lazy initialized attributes with bidirectional relation, hash calculation attempted to fetch data and lopped back infinitely. Since all Foo, Bar and FooBarStatus are unique by id's it is not necerssary to include joined attribtes in hash calculation. Therefore as per https://projectlombok.org/features/EqualsAndHashCode i have added @EqualsAndHashCode.Exclude to bidirectional lazy initialized attributes which excluded the from hash calculation. Example below:
@Entity(name = "Foo")
@Table(name = "FOO")
public class Foo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "FOO_SEQ")
private Long id;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST, mappedBy = "foo", orphanRemoval = true)
@EqualsAndHashCode.Exclude
private Set<FooBarStatus> fooBarStatus;
}
As suggested by @Chris in comment, root cause was that usage of @Data
annotation generated a HashCode calculation method and since Foo, Bar and FooBarStatus had lazy initialized attributes with bidirectional relation, hash calculation attempted to fetch data and lopped back infinitely. Since all Foo, Bar and FooBarStatus are unique by id's it is not necerssary to include joined attribtes in hash calculation. Therefore as per https://projectlombok.org/features/EqualsAndHashCode i have added @EqualsAndHashCode.Exclude to bidirectional lazy initialized attributes which excluded the from hash calculation. Example below:
@Entity(name = "Foo")
@Table(name = "FOO")
public class Foo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "FOO_SEQ")
private Long id;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST, mappedBy = "foo", orphanRemoval = true)
@EqualsAndHashCode.Exclude
private Set<FooBarStatus> fooBarStatus;
}
answered Nov 29 '18 at 9:16
Akka JaworekAkka Jaworek
4872520
4872520
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53523452%2fbidirectional-onetomany-relation-causing-maximum-open-cursors-exceeded%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Firstly, every entity must have an ID and FooBarStatus doesn't. Secondly, use entityManager.persist() which "Make an instance managed and persistent." so you don't have to query again. Fix these basic issues and see where you're at.
– Alan Hay
Nov 28 '18 at 17:38
@AlanHay Foo and Bar are simplified versions of my classes just to represent the issue, of course they do have Id's. Please provide some context why persist should work differently from merge in this case? it seems like a blind shoot
– Akka Jaworek
Nov 28 '18 at 17:58
Sorry I can't guess what is wrong. Why don't you post all the relevant code and use the API properly as a first step.
– Alan Hay
Nov 28 '18 at 18:08
@AlanHay added full code with repository, changed merge to persist, it did not solve the problem.
– Akka Jaworek
Nov 28 '18 at 18:38
2
Looks like you are doing something funky with your hashcode methods, causing a whole bunch of queries to get triggered. You seem to do this on Bar, foo AND UserNotification and User classes. Don't access lazy or any relationship in a hashcode or equality check, especially if you are using maps or sets which use them.
– Chris
Nov 28 '18 at 19:43