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()));
> 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()));