Need help with Hibernate? Read this first:
I was having problems to evict an instance from session, after double checking my classes equals and hashCode I started looking through
the tests that came on the distribution (Hibernate 3.0.5).
So, after looking through the tests of batch fetch I realized that my classes equals and hashCode were fine after being successful on evicting my classes from the session when I used query.list... but if I used query.iterate() and tried to evict the class immediatelly after running the iterator.next() the instance did not get evicted.
My question: is that the expected behaviour of the evict or not?
Sample code below:
Code:
//Problematic one, without Hibernate.initialize
Session s = HibernateUtil.getSessionFactory().openSession();
Transaction t = s.beginTransaction();
String query = "from SomeClass";
Iterator i = s.createQuery(query).iterate();
obj1 = (SomeClass) i.next();
if (s.contains(obj1)) {
System.out.println("should contain");
}
s.evict(obj1);
if (s.contains(obj1)) {
System.out.println("should not contain");
}
else {
System.out.println("ok, it wasn't anymore");
}
t.commit();
s.close();
Code:
//Fine, with Hibernate.initialize
Session s = HibernateUtil.getSessionFactory().openSession();
Transaction t = s.beginTransaction();
String query = "from SomeClass";
Iterator i = s.createQuery(query).iterate();
obj1 = (SomeClass) i.next();
if (s.contains(obj1)) {
System.out.println("should contain");
}
Hibernate.initialize(obj1);
s.evict(obj1);
if (s.contains(obj1)) {
System.out.println("should not contain");
}
else {
System.out.println("ok, it wasn't anymore");
}
t.commit();
s.close();
---------------