-->
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: Determine if an instance exists
PostPosted: Sun Jan 04, 2009 10:36 am 
Newbie

Joined: Sun Jan 04, 2009 10:24 am
Posts: 2
Hi all,
I can't find a proper way to check if a record on db has be deleted.

I have a simple mapped class, with no parent, childs, collections.. just simple properties.
I want simply have a method that check if a particular entity is still on db or not (a row can be deleted from another application or from the db administrator, for example).

I'm using this code:

Code:
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
ZipFile zipFile = (ZipFile) HibernateUtil.getSessionFactory().getCurrentSession().createQuery(
         "select zipFile from ZipFile zipFile where zipFile.name = :fileName")
                                .setParameter("fileName", fileName)
                                .uniqueResult();

HibernateUtil.getSessionFactory().getCurrentSession().close();

return zipFile != null;


The code always retrieve the row even if it's deleted from db..
I tried to work with the cache, using clear(), or to evict the zipFile and to load() or get() the entity with the previously retrieved id.. always the entity is retrieved.

What i'm doin wrong?
Thanks in advance.


Top
 Profile  
 
 Post subject: Re
PostPosted: Mon Jan 05, 2009 4:28 am 
Beginner
Beginner

Joined: Wed Nov 26, 2008 1:02 am
Posts: 22
Hi,

the query will returns null if no row(s ) exist,check whether you have committed modifications (deleting a row in db),then changes of db will reflects to your application.

_________________
if it solves,rate me


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 05, 2009 4:42 am 
Newbie

Joined: Sun Jan 04, 2009 10:24 am
Posts: 2
Hi,

the problem is that it doesn't return null, even if the row in the db doesn't exist anymore (yep the transaction is committed).
Seems that hibernate is using its cache, and I can't figure out how to force a reload from the db.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 05, 2009 4:52 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
Use OpenSession() instead of getSession().

Don't forget to commit ur Transaction and use explicit transaction object.

Truncate ur DB.

Use evict(),flush() & close() methods on session and also use evict() method on SessionFactory.

**) Do u delete record of ur ZipFile from Hibernate or SQL? if u delete record from hibernate, commit ur transaction.

_________________
If u feel it will help you, don't forget to rate me....


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.