Sleep Mode: Parent-to-One Update

I have two classes: Parentand Child, matched one to many with relations belonging Child. I also use second level cache with Ehcache.

To keep in touch, I do this:

child.setParent(parent);
session.saveOrUpdate(child);
parent.getChildren().add(child);

When I load Parentinto another session (from level 2 cache), will this new added be Childvisible? What is the correct way to update the parent collection in this situation?

Bonus points for answers that contain some specific explanation or link to documents, and not "seems fine to me, yes."

To be clear: everything happens inside a transaction that is properly executed. The main question is: Is this method correct for updating parent.childrenthis Sessionfor others in the second level cache?

Another point: What should I do to cut such a collection from the second level cache during rollback?

+5
source share
4 answers

-, - , , , ( , , Hibernate). , , Hibernate, - ( Hibernate, Hibernate).

Transaction tx = session.beginTransaction()

child.setParent(parent);
session.saveOrUpdate(child);
parent.getChildren().add(child);

tx.commit()

:

() : commit() FlushMode . close() . close() JDBC . Java JTA.

0

?

:

child.setParent(parent);
parent.getChildren().add(child);
session.saveOrUpdate(child);
session.flush();

, , . , .

0

, Eager.

:

Parent p = em.find(Parent.class, 1);
Hibernate.initialize(p);
System.out.println(p.child);

.

0

, parent.children(READ_ONLY, NONSTRICT_READ_WRITE...). READ_ONLY, , hibernate .

0

All Articles