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.
This is a pretty informative error message. Primitive types cannot be null. So if you're performing a load from the DB, and the field doesn't have a NOTNULL constraint, you can have null values, which Hibernate will attempt to assign to your variable of primitive type.
The best way to avoid Hibernate's attempts at setting null values to primitives is to use Wrapper classes (Integer, Long, Double...); and especially, if you need to tack on a column or 2 to an existing table. Auto-boxing is your friend.
Java primitive types (such as int , double , or float ) cannot have null values, which you must consider in choosing your result expression and host expression types.
The JPA specification defines that during ordering, NULL values shall be handled in the same way as determined by the SQL standard. The standard specifies that all null values shall be returned before or after all non-null values. It's up to the database to pick one of the two options.
At finally I've get it implementing UserType
of hibernate as explained here: http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch06.html#types-custom
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With