I have two classes: Parent and Child, mapped one-to-many with relationship owned by Child. I also am using 2nd level cache with Ehcache.
To persist the relationship, I do this:
child.setParent(parent);
session.saveOrUpdate(child);
parent.getChildren().add(child);
When I load parent in another session (from 2nd level cache), will this newly added child be visible? What is the proper way to refresh parent’s collection in this situation?
Bonus points for answers that have some concrete explanation or link to docs, not “seems fine to me, yes”.
To be clear: Everything happens inside a transaction that is properly committed. The main question is: Is this the right way to refresh parent.children for this Session and for others in 2nd level cache?
Another point: What shall I do to evict such collection from 2nd level cache on rollback?
In my opinion, this is because of the nature of database isolation, things that are not committed should not be visible to other database connection (in Hibernate language, things that are not committed should not be visible to other Hibernate sessions).
What you need to do to be visible in other Hibernate sessions is to commit the transaction (in Hibernate language, commit the Hibernate session).
I quote from this documentation site
In unmanaged context: