I am working with Teneo/EMF/Hibernate and I've made a simple test.
- I create 2 objects A and B. A has a non-containment relation one-to-many with B (B1).
This is my test code:
// LOAD A Session session = this.hbds.getSessionFactory().openSession(); Transaction transaction = session.beginTransaction(); A loadedA = (A)session.createQuery("FROM A a LEFT JOIN FETCH a.b1").list().get(0); transaction.commit(); session.flush(); session.close(); // LOAD B session = this.hbds.getSessionFactory().openSession(); transaction = session.beginTransaction(); B loadedB = (B)session.get("B", (Long)6L); transaction.commit(); session.flush(); session.close(); // ADD B TO A session = this.hbds.getSessionFactory().openSession(); transaction = session.beginTransaction(); loadedA.getB1().add(loadedB); //loadedA.getB1().add(b); session.saveOrUpdate(loadedA); transaction.commit(); session.flush(); session.close(); this.hbds.close();
- The following ones are my different configurations for the option CASCADE_POLICY_ON_NON_CONTAINMENT in Teneo and the respective error messages I get:
- REFRESH, MERGE, PERSIST: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [B#6]
- REFRESH, MERGE: works, but if I create a new object "b" that is not in the DB and try to add it to A, I get the following error: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: B
Do you know what is the right configuration to work with saveOrUpdate and to avoid these errors? Or any solution to avoid this problem?