Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Cache Refresh Problem(Cache refresh problem when dealing with one-2-many relation update)
Cache Refresh Problem [message #479862] Wed, 12 August 2009 18:28 Go to next message
Rui Tang is currently offline Rui Tang
Messages: 2
Registered: August 2009
Junior Member
Hello friends,

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 Go to previous message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

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


James : Wiki : Book : Blog : Twitter
Previous Topic:Pessimistice locking hint - hanging locks
Next Topic:Conflicting query
Goto Forum:
  


Current Time: Thu Oct 23 10:12:10 GMT 2014

Powered by FUDForum. Page generated in 0.11099 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software