Thursday 2 October 2014

Hibernate - annotation mappings and misleading SerializationException

Took me a few hours to get to the bottom of this.
I was getting the following Exception, which did not make sense as the deSerialization test passed OK.
I have a domain object that I had proved through a unit test that I could serialize & deserialize successfully.


My domain object contained the following object which if I removed then worked OK:


But tomcat & my integration test were both failing with the following Exception.


It turns out I had missed out an annotation, obvious really but I could not spot the error & the Exception was totally misleading. What had I missed? The '@manyToOne' annotation. Twit.

4 comments:

Vance Blakely said...

Hey Bill, thanks very much for this post. I had the exact same issue and was totally confused by the error message that was produced.

After understanding the root cause of this issue, the error message does make a little more sense. It my case, the controller was failed to deserialize the object because of the missing annotation.

Once I added the ManyToOne annotation, everything work perfectly.

Thanks!

Unknown said...

Welcome Post very usefull informatation.and iam expecting more posts like this please keep updating us........

Unknown said...

I have the same Problem; however, it is strange: It does work well on Wildfly 8.2.1. Now I migrated to Wildfly 10.1.0. On my local PC, all works still fine; on OpenShift (same Wildfly Version, same Hibernate Version, ... same Project), this exception is raised, even though I pointed both instances to the same DB...
What can be wrong?
Why do you put the annotaion an the getter rather than on the field itself? Does this matter?
Thanks for your help.

Bill Comer said...

for the last Q - see https://stackoverflow.com/questions/305880/hibernate-annotation-placement-question

As to why, not sure. Firstly ensure you have a clean build. Secondly try reverting back to your 8.2.1 & verify it works again.