-->
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.  [ 6 posts ] 
Author Message
 Post subject: Check if object exists in database WITHOUT using Session.get
PostPosted: Wed Feb 11, 2004 11:09 am 
Beginner
Beginner

Joined: Thu Jan 22, 2004 6:16 am
Posts: 40
Location: Luxembourg
I wonder if it's possible to check if an object exists in the database without using the Session.get(Class clazz, Serializable id) method ?

I want to be able to do this check at anytime in the application. When using the Session.get(Class clazz, Serializable id) method, it's required that the object to check for is associated with the Session. I want to avoid this.

Is this possbly to achieve ?

Regards

Ps. I want this to be quite generic, i.e I don't want to write a lot of SQL/HQL to query the database. Ds.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 11:36 am 
Beginner
Beginner

Joined: Thu Jan 22, 2004 6:16 am
Posts: 40
Location: Luxembourg
Some more explanation. I have the following code where I try to
retrieve an object of class CommsIn ( this is obj in the parameter list )from the database :


public void check( Object obj ) throws PersistenceException
{
Session sess;

try
{

sess = sessionFactory.openSession();

Object o = sess.get(obj.getClass(),sess.getIdentifier(obj));

sess.close();

}
catch ( Throwable e )
{}
finally
{}
}

I got this Exception :

[11/02/04 16:33:41][WFlowP0 ][ERROR] com.hp.mw.csf.AccessManager - cannot create object of type test.CommsIn The instance was not associated with this session

Therefore I wonder, how to check if an object is already in the database at ANY time during runtime without using Session.get-method


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 11:47 am 
Regular
Regular

Joined: Tue Aug 26, 2003 3:09 pm
Posts: 58
Well, first of all, you are calling session.getIdentifier(), which the api docs states:

Quote:
Return the identifier of an entity instance cached by the Session, or throw an exception if the instance is transient or associated with a different Session.


So if you just pass the actual identifier value to session.get(), it will work. Of course, this will result in associating the loaded object with the session. If that's not what you want, then you could do a simple select count(*) from classname where id = ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 12:13 pm 
Beginner
Beginner

Joined: Thu Jan 22, 2004 6:16 am
Posts: 40
Location: Luxembourg
But the identifier has to be Serializable, and in this case it is just a String.

It seems avoiding SQL may be hard in this case :=(


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 12:26 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 3:09 pm
Posts: 58
A String is Serializable. I don't understand what the problem is.[/i]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 12:26 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you can get an object that isn't yet associated to the session.
But you can't session.getIndetifier(obj); on a non associated object.

try
Code:
obj = session.get(myclass, myId);
session.evict(obj);

If you want to evict the object after retrieving it.

Every identifiers must be Serializable in Hibernate.

Code:
Object o = sess.get(obj.getClass(),sess.getIdentifier(obj));

is pretty useless, it'll basically do
Code:
Object o = obj;

_________________
Emmanuel


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