Avoid Hibernate Exception Null has been set to a setter primitive type property without wrappers

The presence of a long field for an object controlled by sleep mode can be dangerous with zero values. When Hibernate tries to set zero to a primitive, an exception will be thrown.

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

But I am working on a project where we cannot use Long or Integer. I am wondering if there is a way to override hibernation types to use the nullSafe method or something like that.

+6
source share
2 answers

Finally, I got the UserType hibernate implementation as described here: http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch06.html#types-custom

+1
source

I always use types of encapsulated classes, but if you cannot check entity attributes before it looks like:

 if (entity.getAttribute == null){ entity.setAttribute(0); //case of long, int... } 
-1
source

Source: https://habr.com/ru/post/982087/


All Articles