Why when using HibernateCriteriaBuilder in Grails is I set to "Null" for the property of a primitive type error message?

I get the following error when using a primitive attribute in my grails domain object:

Null value was assigned to a property of primitive type setter of MyDomain.myAttribute org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of MyDomain.myAttribute at grails.orm.HibernateCriteriaBuilder.invokeMethod(HibernateCriteriaBuilder.java:1077) 
+59
grails gorm
Jul 01 '10 at 1:33
source share
9 answers

According to this SO stream, the solution is to use non-primitive wrapper types; e.g. Integer instead of int .

+94
Jul 01 '10 at 1:33
source share

A null value cannot be assigned to a primitive type, such as int, long, boolean, etc. If the database column corresponding to the field of your object can be zero, then your field should be a wrapper class, for example Integer, Long, Boolean, etc.

The danger is that your code will work fine if there are no zeros in the database, but it will crash after entering zeros.

And you can always return a primitive type from the receiver. Example:

  private Integer num; public void setNum(Integer i) { this.num = i; } public int getNum() { return this.num; } 

But in most cases you need to return a shell class.

Thus, either set the DB column to not allow zeros, or use a wrapper class.

+35
Dec 17
source share

The primitive type cannot be null. Thus, the solution replaces the primitive type with the primitive wrapper class in the tableName.java file. For example:

 @Column(nullable=true, name="client_os_id") private Integer client_os_id; public int getClient_os_id() { return client_os_id; } public void setClient_os_id(int clientOsId) { client_os_id = clientOsId; } 

link http://en.wikipedia.org/wiki/Primitive_wrapper_class to find a wrapper class of a primitive type.

+8
Jan 22 '13 at 3:48 on
source share

use Integer as a type and set the setter / receiver accordingly.

 private Integer num; public Integer getNum()... public void setNum(Integer num)... 
+4
Dec 04 '13 at 21:55
source share

@Dinh Nhat, your setter method looks wrong, because you are again introducing a primitive type, and it should be:

 public void setClient_os_id(Integer clientOsId) { client_os_id = clientOsId; } 
+2
Jul 20 '14 at 9:41
source share

I will try to understand you with an example. Suppose you had a relational table (STUDENT) with two columns and an identifier (int) and NAME (String). Now, as an ORM, you would make an entity class somewhat as follows: -

 package com.kashyap.default; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; /** * @author vaibhav.kashyap * */ @Entity @Table(name = "STUDENT") public class Student implements Serializable { /** * */ private static final long serialVersionUID = -1354919370115428781L; @Id @Column(name = "ID") @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column(name = "NAME") private String name; public Student(){ } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } 

Suppose the table already has entries. Now, if someone asks to add another column "AGE" (int)

ALTER TABLE STUDENT ADD AGE int NULL

You will need to set the default values ​​to NULL to add another column to the pre-populated table. This forces you to add another field to the class. Now the question is whether you will use a primitive data type or a non-primitive wrapper data type to declare a field.

 @Column(name = "AGE") private int age; 

or

 @Column(name = "AGE") private INTEGER age; 

you need to declare the field as a non-primitive wrapper data type, because the container will try to map the table to the entity. Therefore, it will not be able to display NULL values ​​(by default) if you do not declare the field as a wrapper and eventually throw away "Null value was assigned to the primitive type property" Exception ".

+2
Jun 11 '16 at 17:53 on
source share

Verify that the myAttribute database field contains zero instead of zero.

+1
Apr 16 '15 at 11:28
source share

Either completely avoid null in the database via NOT NULL and, in essence, Hibernate via @Column(nullable = false) respectively, or use the Long wrapper instead of the Long primitives.

The primitive is not an object, so u cannot set it to null .

+1
Mar 31 '16 at 13:34
source share

Change the parameter type from the primitive to the object and put the zero check in the installer. Example below

 public void setPhoneNumber(Long phoneNumber) { if (phoneNumber != null) this.phoneNumber = phoneNumber; else this.extension = 0l; } 
+1
Jul 16 '16 at 22:54
source share



All Articles