zoukankan      html  css  js  c++  java
  • 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就不会在未指明默认列的情况下将数据库表中默认值字段清空,但同时也会造成无法对此字段插入或更新非默认值。

  • 相关阅读:
    css3 flex
    多行文本溢出 显示... 判断是否多行文本溢出
    事件多次执行
    WinForm布局
    winform 公共控件
    WinForm窗体菜单和工具栏
    2017-4-24WinForm 基础
    2017-4-20实体类,数据访问类.字符串攻击.防攻击
    ADO.net增删改查
    类库,通用变量,is/as运算符,委托。
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13317922.html
Copyright © 2011-2022 走看看