-->
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.  [ 1 post ] 
Author Message
 Post subject: session.get(): different instances for same row
PostPosted: Thu Dec 12, 2013 5:19 am 
Newbie

Joined: Thu Dec 12, 2013 3:52 am
Posts: 1
Hello,
i have table with primary key column defined as char(4).
The table contains only single row with key value "A" which is physically stored as "A " because it is char(4).
In the code I have (note the id value with and without trailing spaces):

MyTable o1 = (MyTable) session.get(MyTable.class, "A ");
MyTable o2 = (MyTable) session.get(MyTable.class, "A");
MyTable o3 = (MyTable) session.createCriteria(MyTable.class).add(Restrictions.idEq("A ")).setMaxResults(1).uniqueResult();
MyTable o4 = (MyTable) session.createCriteria(MyTable.class).add(Restrictions.idEq("A")).setMaxResults(1).uniqueResult();
System.out.println(o1 == o2);//prints false
System.out.println(o1 == o3);//prints true
System.out.println(o1 == o4);//prints true
System.out.println(o1.getMyKey() + "|" + o2.getMyKey() + "|");//prints A |A|

All instances are surely associated with the same database row (there is only one row in the whole table).
Instances o1, o3 and o4 are the same Java instances (==), instance o2 is different.
Criteria behave consistently - db identity leads to Java identity within session.
Session.get() behaves inconsistently - Java identity is broken within session.

In Hibernate reference i read "Within a Session the application can safely use == to compare objects.".
In the case of session.get() it seems to me that this might not be always true.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.