|Cache Refresh Problem [message #479862]
||Wed, 12 August 2009 18:28
| Rui Tang
Registered: August 2009
I have a bidirectiion one-2-many relationship between A and B.
A is the ONE side, and B is the MANY side.
Now I add an instance of B (say b) into database.
B b = new B();
b.setA(a); // suppose that I've already had an A instance.
session.persist(b); // surround this code I've opened a transaction
Outside this transaction, I open a new one then use
a.getBs(); // the same a in database
to fetch all related B, but I cannot get the set that contains the new added object b.
I also tested that, if I explicitly set a.getBs().add(b); then the result seems right.
But I'm wondering that, when I persist(b), all the relations have been saved in database, but it seems that the cache not refreshed, this case leads a.getBs() cannot get the new added b, because:
1. no database query performed
2. the cache has not been refreshed automatically or manually
So can anyone tell if this is a bug or the common behavior of EclipseLink? Because Hibernate may work OK without manually add b to a, because A is the master side.
Thank you for help!
|Re: Cache Refresh Problem [message #480835 is a reply to message #479862]
||Tue, 18 August 2009 15:41
| James Sutherland
Registered: July 2009
Location: Ottawa, Canada
In JPA you must always add to both sides of the relationship, otherwise |
the objects will be out of synch.
Technically you could refresh the object, or disable caching, and see the
relationship from the database, as the ManyToOne does the writing, but
best to ensure your object model is not corrupt.
Hibernate does not cache by default, so if you created a new EntityManager
you would read from the database. EclipseLink does cache by default, so
you read from the cache. If you use the same EntityManager, even in
Hibernate or without caching your relationship would be empty.
To disable caching see,
http://www.eclipse.org/eclipselink/api/1.1.2/org/eclipse/per sistence/config/PersistenceUnitProperties.html#CACHE_SHARED_ DEFAULT
James : Wiki : Book : Blog : Twitter
Powered by FUDForum
. Page generated in 0.01811 seconds