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.
|