Migrating Hibernate from 3.1 to 3.5.x (3.5.3 actually)

Posted on September 10, 2010


After changing the jar files (Hibernate3, all in \lib\required and any that you use from \lib\optional)

Here’s the migration quide

#1.  I got a java.lang.Long class cast exception.

On further investigation I found that any count(*) query returns Long (not sure what it was doing in 3.1) and I was casting it as Integer. Even the example in hibernate documentation online is incorrect. As you can’t cast Long as Integer – so you need to use intValue

Long count = session.createQuery(“select count(*) from Customer”).uniqueResult();

Integer iCount = new Integer(count.intValue());

#2. Then came org.hibernate.QueryException: illegal attempt to dereference collection

In earlier versions of Hibernate implicity join with a collection was allows. For example if you wrote

select CUST from Customer as CUST where CUST.addresses.addressArea = ?

was legal. Now it throws an exception. The correct query should now be

select CUST from Customer as CUST join CUST.addresses as ADDR where ADDR.addressArea = ?

Posted in: Hibernate