Null value was assigned to a property of primitive

Exception:
org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of com.seven.domain.Book.printTime
緣由:數據庫裏printTime字段爲NULL
Java代碼java

<!--Hibernate映射文件中的字段--> 
<property name="printTime" type="long"><column name="print_time"/></property> 
[java] view plaincopyprint?
<!--Hibernate映射文件中的字段--> 
<property name="printTime" type="long"><column name="print_time"/></property> 
<!--Hibernate映射文件中的字段-->
<property name="printTime" type="long"><column name="print_time"/></property>

其中的類型爲hibernate類型,在生成的Book類中,printTime字段爲long類型,爲基本類型,不能爲NULL.
解決方法:
第一種:數據庫字段不設置爲空;
第二種:手動修改映射文件,printTime使用Java類型Long,即type="java.lang.Long",Book類中的字段也要改成Long。同理,int爲Integer.
第三種:在反向工程時使用Java類型,而不是hibernate類型數據庫

相關文章
相關標籤/搜索