-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Behaviour for Query for non flush data.
PostPosted: Wed Aug 08, 2007 12:36 pm 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
Hi,

Hibernate version:
Annotations: 3.2.0.GA
Core: version 3.2.3
EntityManager: 3.3.1.GA


1. I have an Entity POJO which is Persistent and attached.
2. I update some of its data.
3. I do not flush or commit this to the database.
4. I execute a query for the same POJO. I expect the Query to return what is in the database. So I do not expect to see some of the data I see in 2.
5. The query returns me data not from the database but from the persistence context.

Is this correct behaviour? I would have thought a query should always return what is in the database.

Code:

public void testQueryVersusFlush() {
      EntityManagerFactory emf =
   Persistence.createEntityManagerFactory("h-source");
      EntityManager em = emf.createEntityManager();
      Person person = new Person();
      person.setPk(new EntityOid(3009, 10778));
      em.persist(person);
          
      person.setLastName("OriginalName");
      em.getTransaction().begin();
      em.getTransaction().commit();
     
      person.setLastName("UpdatedName");
       
      // Now query it.
     Query q = em.createQuery("SELECT P from Person P WHERE P.pk.c =" +
             person.getPk().getC() +
             " AND P.pk.i = " + person.getPk().getI());
      System.out.println(">> getSingleResult");
      Person queriedPerson = (Person)q.getSingleResult();
      System.out.println("queriedPerson.getLastName() = " +   queriedPerson.getLastName());
            em.close();
        }


This will output:

queriedPerson.getLastName() = UpdatedName

even in the database, lastname is always OriginalName.

Note, the only thing I could find relevant in the spec was section
3.6.2


"When queries are executed within a transaction, if FlushModeType.AUTO is set on the Query
object, or if the flush mode setting for the persistence context is AUTO (the default) and a flush mode
setting has not been specified for the Query object, the persistence provider is responsible for ensuring
that all updates to the state of all entities in the persistence context which could potentially affect the
result of the query are visible to the processing of the query. The persistence provider implementation
may achieve this by flushing those entities to the database or by some other means."

However I make the change outside a transaction so I am bit confused here.

Comments very much appreciated.[/code]


Last edited by breako on Wed Aug 08, 2007 1:02 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 08, 2007 12:39 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
This is expected, read the documentation about FlushModeType for Entitymanager.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 08, 2007 12:42 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Note that even with a different flush mode and if you don't flush before the query, this behavior will not change in your case. Which is a good thing, because the persistence context raises the isolation guarantees to repeatable read for entity instances. If you execute a scalar query (e.g. just for the persons name), you will get the data from the database.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 08, 2007 1:39 pm 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
Hi,
I checked the spec, relevant part and included it in original post.

My query is not exectued within a transaction so I would have thought the Query reading the same value in the Persistence Context and not in the database would not apply. My understanding is that the spec is not explicit about what the behaviour is, if the Query is executed outside the Tx.

comments appreciated...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 08, 2007 1:54 pm 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
christian wrote:
Note that even with a different flush mode and if you don't flush before the query, this behavior will not change in your case. Which is a good thing, because the persistence context raises the isolation guarantees to repeatable read for entity instances. If you execute a scalar query (e.g. just for the persons name), you will get the data from the database.

Hi,
I re ran the tests changing the flushModeType and I see the same behaviour like you described i.e. the query does not bring back data from database but instead the data from persistence context. Could you elaborate why this is good?
I can't understand your reason.

Also, I updated the test so that instead of doing:

Code:
person.setLastName("UpdatedName");

I did:

Code:
em.remove(person);


the subsequent query will then return what is in the database and the value for last name will be, OriginalName. Is this not inconsistent behaviour?
In one case the Query returns what is in database, the other time it returns what is in Persistence Context?

Many many thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.