Why do I get a “Null value was assigned to a property of primitive type setter of” error message when using HibernateCriteriaBuilder in Grails

后端 未结 12 671
长情又很酷
长情又很酷 2021-01-30 07:58

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         


        
相关标签:
12条回答
  • 2021-01-30 08:34

    use Integer as the type and provide setter/getter accordingly..

    private Integer num;
    
    public Integer getNum()...
    
    public void setNum(Integer num)...
    
    0 讨论(0)
  • 2021-01-30 08:34

    Do not use primitives in your Entity classes, use instead their respective wrappers. That will fix this problem.

    Out of your Entity classes you can use the != null validation for the rest of your code flow.

    0 讨论(0)
  • 2021-01-30 08:38

    Make sure your database myAttribute field contains null instead of zero.

    0 讨论(0)
  • 2021-01-30 08:39

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

    public void setClient_os_id(Integer clientOsId) {
    client_os_id = clientOsId;
    }
    
    0 讨论(0)
  • 2021-01-30 08:42

    Either fully avoid null in DB via NOT NULL and in Hibernate entity via @Column(nullable = false) accordingly or use Long wrapper instead of you long primitives.

    A primitive is not an Object, therefore u can't assign null to it.

    0 讨论(0)
  • 2021-01-30 08:43

    A primitive type cannot be null. So the solution is replace primitive type with primitive wrapper class in your tableName.java file. Such as:

    @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;
    }
    

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

    0 讨论(0)
提交回复
热议问题