-->
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.  [ 3 posts ] 
Author Message
 Post subject: Query over session cache
PostPosted: Tue Apr 12, 2011 3:54 am 
Newbie

Joined: Tue Apr 12, 2011 3:20 am
Posts: 3
I have a situation where in a transaction scope I don't know if some objects has been persisted (and cached in session) or not, so if object doesn't exits I create it at this time. I forced session not to flush until tx commits. If I know the identity of the object, no problem, I perform a find by id with "get" method and the object is retrieved from session cache if exists. My problem is when i don't know the id of that object, then I perform a query over an unique/not id property. But i see this operation makes a dirty read from db and returns null although an object that match that criteria exists in session. In this case hibernate ignores session cache and unique restriction fires cause i'm trying to inserts two rows with the same value on unique field.

There is a way to perform a query over session cache avoiding dirty reads? (i can't flush the session before)


Top
 Profile  
 
 Post subject: Re: Query over session cache
PostPosted: Tue Apr 12, 2011 5:18 am 
Newbie

Joined: Thu Jan 27, 2011 10:53 am
Posts: 12
Hi,
One way is to override the equals method of your bean in order to use the unique property used for the query.
You can have a look et the following link:
http://community.jboss.org/wiki/EqualsandHashCode
Hope it helps.


Top
 Profile  
 
 Post subject: Re: Query over session cache
PostPosted: Tue Apr 12, 2011 8:29 am 
Newbie

Joined: Tue Apr 12, 2011 3:20 am
Posts: 3
Thanks for the quick response.

I have tested your solution but the same thing happens...

Locale a = new Locale("es_ES");//-> Locale is my bean mapped with a surrogate int key id (seq generated) and a unique property String iso2
// passed by constructor arg...

session.saveOrUpdate(a);
.
.
.
Locale b = new Locale("es_ES");//->on same Tx scope and no flush beetween. Here i don't know if locale es_ES has been saved! (obviosly i can make an awful static map to control this, but i'm interested in control hibernate)
session.saveOrUpdate(b);//

When tx commit a unique restriction over iso2 column fires. So Hibernate although bussiness identity is setted on unique property (overriding eq/hC method) attempts to save two times the same object...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.