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]