Why does hibernate fetch objects lazy on access?
I can make this test, which in my opinion is kind of weird:
Code:
public void testTest()
{
Region region = getRegionPersistent();
regionDao.getHibernateTemplate().evict(region);
Country c = null;
try {
c = countryDao.get(region.getId());
System.out.println("found ");
} catch (Exception e) {
e.printStackTrace();
System.out.println("not found");
}
System.out.println(c.getName());
}
The purpose of this test was to insert a Subclass into the db, evict the object from the session so hibernate doesn't return that exact object. Then retrieve another subclass (with the same baseclass) from the db. The idea was to check if hibernate returns the baseclass, or the subclass, or gives an
Hibernate gives an exception which sais that he can't find the object (which is what we expected, because it does not exists).
The strange part is that the not found exception is thrown on the last line, in the system.out when we try to print the name of the object we fetched..
This is kind of strange, since we fetch some object with by Id, try catch if we can find it or not.. It prints "found", but when we actually use the object, suddenly it throws an exception... Is this a bug or a feature to improve performace or something?
Im using:
Hibernate-Version: 3.1.3
Hibernate Annotations Version: 3.1.0.Beta10
Spring-Version: 2.0-rc3