-->
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.  [ 4 posts ] 
Author Message
 Post subject: [NEWBIE] Hibernate does not present actual data
PostPosted: Mon May 24, 2010 4:18 am 
Newbie

Joined: Fri May 21, 2010 2:24 am
Posts: 2
I'm new on using Hibernate so please be bare with me.
Currently I'm using : Netbeans 6.8, MySQL 5, and Hibernate3 with POJO model

I've got a problem which I can describe as follow:
A part of my application is supposed to insert a record to a specified table.
Each record in this table is unique, hence there is a check before inserting the record to find out whether the data is already there in the table.
If it does not exist then the data is inserted, otherwise it won't be inserted into the table.
It works fine.
But then, if I remove a record or all record on the table outside the application scope ( using MySQL command line to delete all records in the table ) the application fails to detect that the record does not exist anymore after deleting.
It keeps saying that the record exists.
It seems that Hibernate persists the query to check for existing record somewhere.

Well...I know I need to dig more about Hibernate :D but meanwhile please enlightening me
TIA


Top
 Profile  
 
 Post subject: Re: [NEWBIE] Hibernate does not present actual data
PostPosted: Mon May 24, 2010 6:49 pm 
Regular
Regular

Joined: Tue May 11, 2010 5:50 pm
Posts: 54
Location: Norman, Ok, U.S.A
Could be caching issue. You can try one of these suggestions, see if the that help:
Set the first-level and second level cache usage to false in the hibernate config file.
Flushing and clearing the session after every insert.

Things that can help:
Override the equal and/or hashCode method in the entity class.

Also to skip to procedure where you have to query the table every time you are inserting the data to see if it already exists, try overring the hashCode method in the entity class. create a hashCode based on some criteria using data you get when you populate the fields, and then setting that as the primary key, since the criteria will always produce the same hashCode if the data is same, at which point you can either skip it, give an exception, or simply update the record


Top
 Profile  
 
 Post subject: Re: [NEWBIE] Hibernate does not present actual data
PostPosted: Tue May 25, 2010 2:35 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
As the previous post by ivrirani said it is likely a caching issue. Hibernate has several different caches. The first-level cache is bound to the session and is mandatory (it can't be disabled it the config file!). The second-level cache can store both entities and query results. The second-level cache need to be configured in the configuration file and for each entity that should be stored in it. Query results can be cached by calling Query.setCacheable() before executing it. Overriding equals() and hashCode() will not affect how things are cached.

So... since the information you are looking can be cached at various places in Hibernate it is hard to give any more advice without seeing your code and without knowing more about your configuration.


Top
 Profile  
 
 Post subject: Re: [NEWBIE] Hibernate does not present actual data
PostPosted: Tue May 25, 2010 2:52 am 
Newbie

Joined: Fri May 21, 2010 2:24 am
Posts: 2
Yes, it was a caching issue and somehow I put incorrect flow to get the session close to make sure the next query will read fresh data instead of from cache.
Managed to get it solved :)
Thank you very much for replying.


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