Tuesday, March 17, 2015

JPA error: "Null value was assigned to a property of primitive type setter of MyDomain.myAttribute"

I have a database table, one of the columns is REF_ID(INTEGER), and this column can be set as null. When I used Eclipse JPA tool to generate JPA Entities from tables, this attribute was generated as this.

 @Column(name="REF_ID")  
      private int refId;  

When I tested it, and set null value to this attribute, I got the error "Null value was assigned to a property of primitive type setter of MyDomain.myAttribute".

To solve the problem, I just simply changed the int data type to Integer. Then everything works perfectly fine.

A null value cannot be assigned to a primitive type, like int, long, boolean, etc. If the database column that corresponds to the field in your object can be null, then your field should be a wrapper class, like Integer, Long, Boolean, etc.

No comments:

Post a Comment