Friday, July 16, 2010

Hibernate error : Cannot instantiate abstract class or interface:

If you ever saw this error message "Cannot instantiate abstract class or interface: " in retrieving a list of objects, there could be one of the two reasons.

Assuming you have an object model where there's an abstract parent entity which is inherited by a list of children entities and each entity is mapped to a table, then

1) First make sure that records are consistent. That is, when you try to retrieve a record, hibernate is retrieving a list of parent objects and according to the data model design, each parent object must have subsequent child object. So, for each row in a parent table, there should be a matching record in the sub-table. For some reasons, if the record is missing the sub-table, when hibernate returns the data, it will throw the exception since it cannot instantiate the abstract parent class.

2) It is possible that your data are intact, but the problem is the changes of the data model in the code base. In our class, we removed one subclass but we forget to migrate the data and remove the sub-table that is being mapped to the subclass. Hence, when hibernate retrieves a list of parent objects, it is seeing the right data but since our data model are not consistent, it's throwing an exception.

3 comments: