日期:2014-05-16  浏览次数:20334 次

hibernate 插入为空时保留数据库默认值

在插入数据时,如果数据库有默认值为1,且该字段不能为空,但插入值为null,则自动插入数据库默认值;如果插入值不为空则保留该值。

1.使用HBM文件时,加入

dynamic-insert= "true  dynamic-update= "true "

?

2.使用注解时,在类级别上添加

@org.hibernate.annotations.Entity(dynamicUpdate=true,dynamicInsert=true)

?如果没有添加以上内容,hibernate在生成的sql是会为该字段插入一个为null的值,且不会使用默认值,所以hibernate会报错说,不能插入为空的字段。

?