-->
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.  [ 8 posts ] 
Author Message
 Post subject: How can I know if my object is already in Session
PostPosted: Fri Jul 30, 2004 3:05 pm 
Regular
Regular

Joined: Mon Jun 14, 2004 1:42 pm
Posts: 80
Location: Brazil
I have a detached object ob of class X. I may have an object dbob of Class X with same identifier attached to the session.
When I use
session.contains(ob);
if will return true only if ob==dbob? or if ob.equals(dbob) it will be true?
if the first answer is correct how could I check if ob is already associated without hitting the db, something like the private session method getEntity(key)==null ?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 3:38 pm 
Beginner
Beginner

Joined: Wed Jul 21, 2004 12:28 pm
Posts: 27
Location: New York
I am not sure if I can follow but the only way I can make sense out of your question is if I assume that you have two object instances inside your JVM but they represent the same database entity.

so what you can do is:

Code:
session.get(ob.class, ob.getId ());


if your dbob is in Session already session will return it, however if it is not in session it will go to the database to retrieve it. So it might not be what you want to do.

if however you want to check if object with the same database identity exists in the session already but has different java identity, you might want to do this:

Code:
session.lock (ob, LockMode.NONE);


this will throw an exception (net.sf.hibernate.NonUniqueObjectException)
which tells you that there is dbob already associated with session but ob is different java object instance, that has the same database identity as dbob. Of course if you have no exception then there was no dbob in session and you have ob associated with your session now. So if you want to deattach it just run session.evict (ob).

_________________
Thanks,
Alex


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 3:42 pm 
Beginner
Beginner

Joined: Wed Jul 21, 2004 12:28 pm
Posts: 27
Location: New York
another good method to help out with the same thing is

getCurrentLockMode () of session object

Also, buy Hibernate in Action book I found it has very clear explanation of the differences between database identity and JVM identity.

_________________
Thanks,
Alex


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 4:25 pm 
Regular
Regular

Joined: Mon Jun 14, 2004 1:42 pm
Posts: 80
Location: Brazil
Hi tetrione.
Thanks for the help, you understood the problem very well. But your solution will not solve the problem.

Code:
session.get(ob.class, ob.getId ());


will hit DB if dbob does not exist. I don't want to hit the DB each time I want to know if a Object is associated with my Session.

the second idea is good, but I don't like to throw exceptions when I'm just trying to check if the object is already in the session. Indeed it is exactly this exception I'm trying to avoid. Exceptions can cause problems. I can use this method, but again, it's not a good solution.

Code:
session.lock (ob, LockMode.NONE);


getCurrentLockMode(ob) will also throw exception, since ob is not yet attached.

What I want to do is:

Code:
if exists object dbobj in the session (but please, don't go to database to check this,nor throw slow exceptions ) with id=x
  ob=dbob;
  // or copy props from ob to dbob.
else {
  ob = new X();
  ob.pk=myid;
  lock ob;
}


In this particular case I'm worried in db hit becouse ob is just a not null property of another object that was changed. I'm not going to save it, just lock. But another part of my business may had changed ob. Or may not.

Is the book complete ? I got some demo chapters.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 31, 2004 1:31 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
have you looked at the session API. Try session.contains(object o) .


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 31, 2004 10:32 am 
Beginner
Beginner

Joined: Wed Jul 21, 2004 12:28 pm
Posts: 27
Location: New York
contains is not exactly what he needs, because the object might not be in session but object with the id of the object might be in.

in other words what he needs is to do something like get () but if object with given identifier is not in session do not send sql query retrieving it from db, because he wants to associate existing object with the session.

Anyway, "atorres" (sorry don't know your name) I still can't see why causing hibernate throw an exception and catching it is a problem. The performance difference is only making hibenrate to create exception and that in modern JVM is on the order of nanoseconds. I think it is your best choice, because as far as I can see in the API there is no single method with the semantics that you need, even though functionality is there.

_________________
Thanks,
Alex


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 02, 2004 9:38 am 
Regular
Regular

Joined: Mon Jun 14, 2004 1:42 pm
Posts: 80
Location: Brazil
I david!
I agree with you about performance. What worried me is this warning in the reference guide:

Quote:
9.7.4. Exception handling
If the Session throws an exception (including any SQLException), you should immediately rollback the transaction,
call Session.close() and discard the Session instance. Certain methods of Session will not leave the
session in a consistent state.


Well, I think I become a little scared about what exceptions will damage the session context. But I belive this is not the case (the chapter is about closing sessions...). I'm going to try the lock method.

Thanks for the help on this subject!

_________________
Alexandre Torres
--------------------


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 29, 2005 3:32 am 
Newbie

Joined: Wed May 25, 2005 11:03 am
Posts: 13
Hi,

I have a similar problem.

Only difference is that I want to attach as object with the session (for saving) if it is not already associated with it. In this case is it ok to write following code:

try {
session.save(stepResponse);
logger.debug("Object " + stepResponse + " is associated with the
session");
} catch (HibernateException he) {
logger.info("Object " + stepResponse + " was alreay associated with
session");
}

Catching an exception like this could cause harm to session. What exactly could happen???


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