How to handle database null values in Hibernate Application?

前端 未结 3 984
长情又很酷
长情又很酷 2021-01-21 05:54

I am getting \" org.hibernate.PropertyAccessException\" due to null values in my database table. How to handle the exception?

My files are

FetchTest.java

相关标签:
3条回答
  • 2021-01-21 06:12

    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.

    0 讨论(0)
  • 2021-01-21 06:14

    I would personally recommend to actually "clean" the database values, maybe setting the column not nullable.

    But if this can't be done, what you can do is modify your setters, so that it checks for null:

    public void setComm(Double comm) {
        if(null != comm){
            this.comm = comm;
    
        }else{
            this.comm = 0;
        }
    }
    

    hope this helps

    0 讨论(0)
  • 2021-01-21 06:22

    Your mgr-Property is int, which does not allow null. Change it to Integer, which allows null. Or make a default value for your mgr column.

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