1. 程式人生 > >hibernate 對映檔案配置預設值方法

hibernate 對映檔案配置預設值方法

               

問題描述:    hibernate技術中對應資料庫中每一個表,都會有一個對映檔案與之對應,此檔案描述資料庫表中每一個欄位的型別、長度、是否可空等屬性。在進行表中記錄的插入(更新)操作時,hibernate會根據對映檔案中的描述自動生成一個包含所有欄位的插入(更新)sql語句,此時如果對映檔案中某欄位的值為空(NULL)而其在資料庫表中定義的預設值不為空,hibernate會將空值插入到表中,而不會使用此欄位的預設值。

解決方法:    在hibernate對映檔案對資料庫表的描述中,加入dynamic-insert="true"和 dynamic-update="true" 語句,這時hibernate在進行插入(更新)操作時,只會為那些值不為空的欄位賦值,而值為空的欄位就會使用資料庫表中定義的預設值了。

這個表引用了另外的一張表。自己任意建立一張表即可。

關鍵是hibernate對映檔案的class處 dynamic-insert="true" dynamic-update="true" 和property 裡面的insert="false" update="false" 。

兩處都要配置!

<property></property>標籤:

3.1裡面有一個屬性:update=”true|false”

如果設定為false,則在hibernate的update語句裡面沒有<property>標籤所指明的屬性所對應的欄位。

同理,insert=”true|false”

如果設定為false,則在hibernate的insert語句裡面沒有<property>標籤所指明的屬性所對應的欄位。

這樣的弊端是無法從表單上填寫資訊了。

建立資料庫表:

/*--20、人才招聘.招聘資訊表*/create table t_company_position(id integer(10) primary key auto_increment,company_name varchar(50) not null ,position_information varchar(500) not null,position_type varchar(50) ,position_name varchar(50) ,province varchar(10) ,city varchar(10) ,  start_date timestamp,in_date varchar(10) default '長期有效',dimensions varchar(10) , position_number varchar(10) not null default '若干名', position_salary varchar(30) not null default '面議', position_sex varchar(10) not null default '無',position_age varchar(10) not null default '無',education varchar(10) not null default '無',work_experience varchar(10) not null default '無',work_way varchar(10) not null default '不限',position_describe varchar(500) not null default '無',company_describe varchar(1000) not null default '暫無該公司資訊', contact_way varchar(200) not null default '無', company_id integer(10) not null , constraint company_id_fk foreign key(company_id) references t_company_regedit(id));

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!--     Mapping file autogenerated by MyEclipse Persistence Tools--><hibernate-mapping>    <class name="com.meemei.domain.TCompanyPosition" table="t_company_position" catalog="meemei"     dynamic-insert="true" dynamic-update="true" lazy="false">        <id name="id" type="java.lang.Integer">            <column name="id" />            <generator class="native" />        </id>        <many-to-one name="TCompanyRegedit" class="com.meemei.domain.TCompanyRegedit" fetch="select">            <column name="company_id" not-null="true" />        </many-to-one>               <property name="companyName" type="java.lang.String">            <column name="company_name" length="50" not-null="true" />        </property>        <property name="positionInformation" type="java.lang.String">            <column name="position_information" length="500" not-null="true" />        </property>        <property name="positionType" type="java.lang.String">            <column name="position_type" length="50" />        </property>        <property name="positionName" type="java.lang.String">            <column name="position_name" length="50" />        </property>        <property name="province" type="java.lang.String">            <column name="province" length="10" />        </property>        <property name="city" type="java.lang.String">            <column name="city" length="10" />        </property>        <property name="startDate" type="java.util.Date">            <column name="start_date" length="0" />        </property>        <property name="inDate" type="java.lang.String" insert="false" update="false" >            <column name="in_date" length="10" not-null="true"/>        </property>        <property name="dimensions" type="java.lang.String" insert="false" update="false" >            <column name="dimensions" length="10" not-null="true"/>        </property>        <property name="positionNumber" type="java.lang.String" insert="false" update="false" >            <column name="position_number" length="10" not-null="true" />        </property>        <property name="positionSalary" type="java.lang.String" insert="false" update="false" >            <column name="position_salary" length="30" not-null="true" />        </property>        <property name="positionSex" type="java.lang.String" insert="false" update="false" >            <column name="position_sex" length="10" not-null="true"   />        </property>        <property name="positionAge" type="java.lang.String" insert="false" update="false" >            <column name="position_age" length="10" not-null="true" />        </property>        <property name="education" type="java.lang.String" insert="false" update="false" >            <column name="education" length="10" not-null="true" />        </property>        <property name="workExperience" type="java.lang.String" insert="false" update="false" >            <column name="work_experience" length="10" not-null="true" />        </property>        <property name="workWay" type="java.lang.String" insert="false" update="false" >            <column name="work_way" length="10" not-null="true" />        </property>        <property name="positionDescribe" type="java.lang.String" insert="false" update="false" >            <column name="position_describe" length="500" not-null="true" />        </property>        <property name="companyDescribe" type="java.lang.String" insert="false" update="false" >            <column name="company_describe" length="1000" not-null="true" />        </property>        <property name="contactWay" type="java.lang.String" insert="false" update="false" >            <column name="contact_way" length="200" not-null="true" />        </property>        <set name="TResume" order-by="id" inverse="true" table="t_company_position">        <key>           <column name="company_position_id"></column>        </key>        <one-to-many class="com.meemei.domain.TResume"/>        </set>    </class></hibernate-mapping>

注:insert="false" update="false" 的作用是不對當前欄位進行insert和update操作,這樣hibernate就不會在未指明預設列的情況下將資料庫表中預設值欄位清空,但同時也會造成無法對此欄位插入或更新非預設值。