-->
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: Get the same entities?
PostPosted: Sun Mar 25, 2007 9:02 pm 
Newbie

Joined: Sun Mar 25, 2007 8:53 pm
Posts: 2
i want to get the same entities upon the ID, such as follows:

Code:
Userlist ul1= (Userlist)session.get(Userlist.class, new Integer(1));
Userlist ul2= (Userlist)session.get(Userlist.class, new Integer(1));
assertEquals(ul1, ul2);


The test can pass and ul1 is equal to ul2 as objects.

However, if the objects are created from different sessions, the test can not pass, as this:
Code:
Session session1 =HibernateUtil.getSessionFactory().openSession();
Session session2 =HibernateUtil.getSessionFactory().openSession();
      
Userlist ul1= (Userlist)session1.get(Userlist.class, new Integer(1));
Userlist ul2= (Userlist)session2.get(Userlist.class, new Integer(1));
assertEquals(ul1, ul2);


So, my question: is there any method that can allow me created entities from different sessions?

Or if I use the just one session for the whole application, what gonna happen for the system performance? slow?

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 3:56 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Did you implement the equals() and hashCode(), cause that's exactly what should be done and make your test pass here...

Session is a short-lived object, so you shouldn't consider keeping it a long time. There's some pattern to make it possible (disconnecting it, and reconnecting, and so on), but that's not the simplest one.

What's your architecture btw? Web? Swing/Swt app?

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 12:04 pm 
Newbie

Joined: Sun Mar 25, 2007 8:53 pm
Posts: 2
I did not implement any equals() and hashCode(). I just use what Hiberate provide in default. What I want to do is:
If I give an ID, then I can get the exactly the entity object if it is created. If it is created, then no new instance of the entity should be created anymore. If it is not created, then it will be created and returned.

The callers could be many other beans. and Hibernate needs work as a pooling of entities. When I need the entity instance, it gives me the reference of the one I want, but not in multiple instances. The entity should be a singleton upon its ID. I remeber that Hibernate promise provides such function.

Probably, I need it in Web context, but currently we do not decide yet. Do you know any method that I can implement this?

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.