-->
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: Session.contains doesn't work
PostPosted: Thu Nov 02, 2006 7:26 am 
Beginner
Beginner

Joined: Thu Aug 31, 2006 7:41 am
Posts: 24
Hi,

I've got a problem with the contains method of the Session class. It seems it's not working at all.

Code:
org.hibernate.Session hibsession = HibernateUtil.getCurrentSession();
hibsession.beginTransaction();
Instruction in = (Instruction)session.getAttribute("instr");
if(hibsession.contains(in))
  hibsession.merge(in);
else
  hibsession.saveOrUpdate(in);


This code always throws an exception telling me the object already exists in this session. The stacktrace shows that saveOrUpdate was called and not merge.

Anybody seen this before or knows what I might do wrong?

Andreas


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 10:21 am 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
Did you implement equals and hashCode in the class you want to check?

_________________
Please don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 10:56 am 
Beginner
Beginner

Joined: Thu Aug 31, 2006 7:41 am
Posts: 24
MikePloed wrote:
Did you implement equals and hashCode in the class you want to check?


No, and it seems I missed that paragraph in the hibernate reference :(

But why does Session not use the same algorithm for contains as for saveOrUpdate (algorithm to check wether an object is already associated with the session)?

Andreas


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 11:29 am 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
These are two kinds of things. saveOrUpdate checks if an entity is transient (which means has it been already saved to the database). Contains checks, if the entity is bound to the session. An entity can be persistet (= not transient) but not bound to the session.

_________________
Please don't forget to rate


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.