Search code examples
javahibernatehibernate-mappinghbmxml

Avoid "Hibernate exception Null value was assigned to a property of primitive type setter" without wrappers


Having a long field on an entity managed by hibernate could be dangerous with null values. When Hibernate tries to set a null into a primitive an exception will be thrown.

Best solution should be to use Long instead of long, so null can be assigned to Long field.

But, I'm working on a project where we can't use Long or Integer types. I'm wondering if there is a way to override hibernate types to use a nullSafe method or something like that.


Solution

  • At finally I've get it implementing UserTypeof hibernate as explained here: http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch06.html#types-custom