Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / October 2006

Tip: Looking for answers? Try searching our database.

hibernate and instance aliasing

Thread view: 
xyzzy12@hotmail.com - 18 Oct 2006 20:26 GMT
We are having problems with instance aliasing in our application.  I
was hoping that the secondary cache would fix the problem.  But alas it
doesn't.  Basically we create an object A and store it.  Another piece
of code in the same JVM does a query and finds object A and
instanciates it.  However, the application now has two instances of the
same "object" (or db record.)  We need it to return the same instance.
Does hibernate support this mode?

       AliasTest at = new AliasTest();
       at.setName("name1");
       at.setNumber(4);
       {
           Session session = factory.openSession();
           Transaction tx = session.beginTransaction();
           session.save(at);
           session.saveOrUpdate(at);
           tx.commit();
           session.close();
       }
       System.out.println("AliasTest=" + at);
       AliasTest mimic;
       {
           Session session = factory.openSession();
           mimic = (AliasTest)session.get(AliasTest.class,
at.getPid());
           System.out.println("mimic=" + mimic);
           System.out.println("mimic same as prev aliasTest?" + (mimic
== at));
           session.close();
       }
       System.out.println("mimic=" + mimic);
       mimic.setNumber(45);
       System.out.println("mimic=" + mimic);
       System.out.println("AliasTest=" + at);
       System.out.println("mimic equals?"+mimic.equals(at));
       System.out.println("mimic same as prev aliasTest via getNumber
check?"
           + (mimic.getNumber() == at.getNumber()));
Christian Ashby - 19 Oct 2006 10:07 GMT
> We are having problems with instance aliasing in our application.  I
> was hoping that the secondary cache would fix the problem.  But alas it
[quoted text clipped - 3 lines]
> same "object" (or db record.)  We need it to return the same instance.
> Does hibernate support this mode?

If (like the hibernate doc's factory) your factory class opens only one
session per thread, then the secondary cache should indeed only return
one object instance.

The problem is with your AliasTest object. I'd imagine that you haven't
overridden the equals() and hashcode() methods in it.

Please see http://www.hibernate.org/109.html for more details - this is
a well known by-product of using the proxying method of object
instanciation.

Hope this helps,

Christian Ashby
Spiralinks, Inc.

>         AliasTest at = new AliasTest();
>         at.setName("name1");
[quoted text clipped - 26 lines]
> check?"
>             + (mimic.getNumber() == at.getNumber()));


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.