Hibernate 中处理Oracle的字段默认值

方法一:

1、在hibernate的xml配置文件中对有默认值字段的property添加(insert="false" update="false"):

<property name="N_ISOK" type="java.lang.Long" insert="false" update="false">
            <column name="N_ISOK" precision="22" scale="0" />
</property>

注:insert="false" update="false" 的作用是不对当前字段进行insert和update操作,这样hibernate就不会在未指明默认列的情况下将数据库表中默认值字段清空,但同时也会造成无法对此字段插入或更新非默认值。

2、数据库中表字段必须设置默认值:

N_ISOK     NUMBER(2) default 1

方法二(推荐):

在hibernate.xml中的class加入dynamic-insert="true" dynamic-update="true"

如:<class name="com.hibernate.bean.TLoginUser" table="T_LOGIN_USER" schema="CALLERMMS" dynamic-insert="true" dynamic-update="true">

注:dynamic-insert="true" dynamic-update="true" 的作用是当HQL语句中未指明的列将不进行insert和update操作,这样hibernate就不会在未指明默认列的情况下将数据库表中默认值字段清空。

猜你喜欢

转载自elan1986.iteye.com/blog/1102124