Friday, October 05, 2007

Hibernate InstantiationException

Sometimes our own carelessness is our downfall. I have an application where I am using Hibernate to handle persistence. I have been working on this application for about a year now and have grown quite fond of Hibernate.

I have an inheritance hierarchy with an abstract class called Case and two concrete sub classes named MedstarCase and RoadsideCase. I am using the table per subclass strategy for persisting these objects. This worked fine until suddenly this morning, code that worked all along blew up.

I got org.hibernate.InstantiationException
Cannot instantiate abstract class or interface:! I immediately revisited my mappings and painstakingly ran tests to determine the cause. But I found nothing to give me any clues. So I paused to think. What did I do this morning to my code.

I added a new collection to the Case. But the Case already had several collections and none of them caused problems before...and I checked that the mappings were similar.

I had re-imported some data into the database. Aha! I turned to the database to check my data. I had cleared the entire database and re-imported data to run some tests on the data migration code that I would need this coming weekend. I counted the number of rows in the Case table and the number of rows in the medstarcase and roadsidecase tables and found1367 Cases but only 600 medstar cases.

All along I was trying to instantiate Cases that did not correspond to any concrete class. Needless to say this was carelessness on my part for not verifying my data. I keep learning these valuable lessons the hard way though.

After clearing the data, fixing the data import program and re-importing the data, everything worked fine.

6 comments:

Mira said...

Thanks
I ran into similar problem and was bewildered for a day till I ran into your posts.
one headache less.
thanks again!

koallen said...

Thanks for posting this, you saved me a lot of time.

Unknown said...

Thanks a lot!!!
You saved me a day's work with this post.

Raj said...

I ran into the same issue. Really you save my time too.Thank you for your posting.Keep posting issues you faced.

Unknown said...

One more soul saved. Thank you so much ;)

miheys said...

Thanks a lot!!! My case, exactly after data migration.

(Only exception was more confusing: LazyInitializationException: illegal access to loading collection)