Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Find after persist - ignoring cache
Find after persist - ignoring cache [message #810028] Wed, 29 February 2012 15:35 Go to next message
Mauro Flores is currently offline Mauro FloresFriend
Messages: 84
Registered: September 2009
Location: Brasil
Member
I have an entity Foo with an eager manytoone relationship with Boo. I run the following code and the System.out shows null:

    em.getTransaction().begin();
    Foo f = new Foo();
    Boo b = new Boo();
    b.setId(5659);
    f.setBoo(b);
    em.persist(f);
    Foo fret = em.find(Foo.class, f.getId());
    System.out.println(f.getBoo().getName());
    em.getTransaction().commit();


The find method brought de Foo object from the cache, I wished a new query was execute bringing all the propeties of the Boo.

Even If I replace the find by the following query the result is the same:
Query q = em.createQuery("SELECT  f from Foo f where f.id = " + f.getId()).setHint(QueryHints.CACHE_USAGE, CacheUsage.DoNotCheckCache);
Foo fret = (Foo) q.getSingleResult();


If I make the query or the find after the commit, the content of the property name of Boo is shown as expected.

How can I have the same behaviour before executing the commit?
Re: Find after persist - ignoring cache [message #810158 is a reply to message #810028] Wed, 29 February 2012 19:15 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
You are setting an existing Boo id into a new instance of Boo and associating it to a new Foo instance. This foo instance is cached, and so your find method returns the very same instance you just called persist on. You will get the same results if you call System.out.println(f.getBoo().getName()) before the persist call as you do after the call - JPA is using regular java objects. In this case because you did not read in Boo from the database, it does not have any information populated. You will get the correct data if you flush the entityManager and then refresh the foo instance - this will force the relationship and the correct Boo instance to be read in from the database.

Instead of creating an empty Boo instance, try using Boo b = em.getReference(Boo.class, 5659) or a call to find the Boo instance if you want the query done upfront.

Best Regards,
Chris
Re: Find after persist - ignoring cache [message #810792 is a reply to message #810158] Thu, 01 March 2012 15:04 Go to previous messageGo to next message
Mauro Flores is currently offline Mauro FloresFriend
Messages: 84
Registered: September 2009
Location: Brasil
Member
Quote:
Instead of creating an empty Boo instance, try using Boo b = em.getReference(Boo.class, 5659) or a call to find the Boo instance if you want the query done upfront.


Thanks. I'm going to do that.

Quote:
You will get the correct data if you flush the entityManager and then refresh the foo instance - this will force the relationship and the correct Boo instance to be read in from the database.


Do you mean coding flush between persist and find? Like that:
    em.persist(f);
    em.flush();
    Foo fret = em.find(Foo.class, f.getId());


It didn't work either!
Re: Find after persist - ignoring cache [message #810878 is a reply to message #810792] Thu, 01 March 2012 16:35 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

You need to refresh(), but in general use find or getReference, don't create a new Boo instance for an existing object.

James : Wiki : Book : Blog : Twitter
Previous Topic:How to share EntityManagerFactory when different MetaDataSource per tanant
Next Topic:Absence of @DiscriminatorColumn causes exception: Unknown column DTYPE
Goto Forum:
  


Current Time: Fri Apr 19 05:14:38 GMT 2024

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

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

Back to the top