Tuesday, May 10, 2011

Class cast exception while casting the list object to the domain object

Problem:
TestDomainObj test = (TestDomainObj) session.createSQLQuery(query).list();  // class cast exception occurs

Solution:
(TestDomainObj) session.createSQLQuery(query)
.addEntity(TestDomainObj.class)
.list();

CreateQuery returns an java.lang.Object array. We can't cast it to our mapped object.
AddEntity fills the mapped object to the appropriate value, so that we can now use our mapped object other than object array

No comments:

Post a Comment