hibernate 映射文件配置默认值方法

 
【转自: http://www.blogjava.net/keweibo/articles/353872.html】

通过设置hibernate映射文件的class处 dynamic-insert="true" dynamic-update="true" 和property 里面的insert="false" update="false" 实现 .

两处都要配置!

<property></property>标签属性:update=”true|false”

如果设置为false,则在hibernate的update语句里面没有<property>标签所指明的属性所对应的字段。

同理,insert=”true|false”

如果设置为false,则在hibernate的insert语句里面没有<property>标签所指明的属性所对应的字段。

这样的弊端是无法从表单上填写信息了。

<hibernate-mapping>
    <class name="org.gecs.hibernate.test.AdDepartment" table="AD_DEPARTMENT" schema="BARCODE"
           dynamic-insert="true" dynamic-update="true">
        <id name="adDepartmentId" type="long">
            <column name="AD_DEPARTMENT_ID" precision="22" scale="0" />
            <generator class="sequence">
                <param name="sequence">AD_DEPARTMENT_SEQ</param>
            </generator>
        </id>
        <property name="departmentName" type="string">
            <column name="DEPARTMENT_NAME" length="50" not-null="true" />
        </property>
        <property name="active" type="java.lang.Character" insert="false" update="true">
            <column name="ACTIVE" length="1" />
        </property>
        <property name="createdTime" type="date" insert="false" update="false">
            <column name="CREATED_TIME" length="7" />
        </property>
        <property name="createdUser" type="string">
            <column name="CREATED_USER" length="20" not-null="true" />
        </property>

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

你可能感兴趣的:(hibernate 映射文件配置默认值方法)