-->
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.  [ 3 posts ] 
Author Message
 Post subject: Unique object instance for query result
PostPosted: Thu Sep 29, 2005 3:06 pm 
Newbie

Joined: Thu Sep 29, 2005 2:45 pm
Posts: 2
Hello,

I'm new to hibernate and I'm not sure how to change the following behavior.

When I run a query and get the unique result I get the same instance each time the query is called. For example,

Criteria criteria=Session.createCriteria(Person.class);
criteria.add(Restriction.eq("Id",1));
Person p=criteria.uniqueResult();
System.out.println(p.getName()+" "p.toString());

generates
John Smith Person@f961d5

At another point in the application I perform the same query and I get the same person, which is correct, but I also get the same instance of the Person object. So the output would also say

John Smith Person@f961d5

Not
John Smith Person@b764a1

for example. So how do I get the same person object in two different instances of Person?

I hope this makes sense,

Thanks,
Joe


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 29, 2005 4:06 pm 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
make sure your equals and hashcode methods are implemented per the following...
http://hibernate.org/109.html


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Fri Sep 30, 2005 10:25 am 
Newbie

Joined: Thu Sep 29, 2005 2:45 pm
Posts: 2
Thank you very much for that article. It really cleared up what hibernate is doing under the covers. I had to make a wrapper for the hibernate class so that I can get unique instances each time. The wrapper will store custom values that are not intended to be written to the database and maintains the concept of one instance per record per session.

Thanks,


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