-->
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: Strange behaviour HQL
PostPosted: Fri May 06, 2011 9:29 am 
Newbie

Joined: Fri May 06, 2011 9:10 am
Posts: 3
Hi,

I have an entity object in persistence context, say a of type A with property a.prop = 'value1' and a.id = <id>

A a = new A();
a.setProp('value1');
em.persist(a);

Then, em.find(A.class, <id>).getProp().equals('value1') as expected. Also changing prop works as expected:

a.setProp('value2') and em.find(A.class, <id>).getProp().equals('value2')

But I can't explain the following (after setting prop='value2'):

List<A> list = em.createQuery("from A a where a.prop = 'value2'").getResultList() gives an empty list, and
List<A> list = em.createQuery("from A a where a.prop = 'value1'").getResultList().get(0).getProp().equals('value2')

(Everything is running inside same transaction)

I get this behaviour when running integration tests with Spring 2.5.6, Hibernate 3.2.6, JOTM and JtaTransactionManager.


Top
 Profile  
 
 Post subject: Re: Strange behaviour HQL
PostPosted: Fri May 06, 2011 9:40 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
This is because the information in the database and the session first-level cache are different. You'll need to make sure that the session is flushed before executing the query. Setting the flush mode to FlushMode.AUTO is one option, or you can call flush() in your code.


Top
 Profile  
 
 Post subject: Re: Strange behaviour HQL
PostPosted: Fri May 06, 2011 10:02 am 
Newbie

Joined: Fri May 06, 2011 9:10 am
Posts: 3
Strange, this worked using JpaTransactionManager and RESOURCE_LOCAL persistence context. Now, with JtaTransactionManager and JOTM this does not work anymore. Anyway, how can I set flushmode on EntityManager/TransactionManager? I do not want to change test target code since the code is working running in the application server.


Top
 Profile  
 
 Post subject: Re: Strange behaviour HQL
PostPosted: Fri May 06, 2011 12:25 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I have not used Hibernate in JPA mode so things may be different. I found this documentation about flushing http://docs.jboss.org/hibernate/entitym ... e/#d0e1424

It is for a newer version of Hibernate than you are using so things may be different in your version.


Top
 Profile  
 
 Post subject: Re: Strange behaviour HQL
PostPosted: Mon May 09, 2011 2:12 am 
Newbie

Joined: Fri May 06, 2011 9:10 am
Posts: 3
The problem was actually that an Exception was thrown (and catched) in the test method.

public void test...() {
try {
target.doSomethingThatThrowsRuntimeException();
fail("Did not throw exception);
} catch (Exception e) {
assertNotNull(e.getMessage());
}
// Code which caused the "problem" was here
}

The transaction was not active so the DB was not in sync since flush was not executed. Moving the code to a new test method worked fine!


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.