问题描述:
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就不会在未指明默认列的情况下将数据库表中默认值字段清空,但同时也会造成无法对此字段插入或更新非默认值。
分享到:
相关推荐
hibernate实体映射文件字段设置默认值
Hibernate的配置文件,包含了连接持久层与映射文件所需的基本信息,其配置文件有两种格式,具 体如下: ● 一种是properties 属性文件格式的配置文件,它使用键值对的形式存放信息,默认文件名称 为 hibernate....
2.4.3 Hibernate映射类型接口 2.4.4 可供扩展的接口 2.5 小结 2.6 思考题 第3章 第一个Hibernate应用 3.1 创建Hibernate的配置文件 3.2 创建持久化类 3.3 创建数据库Schema 3.4 创建对象-关系映射...
映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射Person类 1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 1.3.4. 值...
2.4.3 Hibernate映射类型接口 2.4.4 可供扩展的接口 2.5 小结 2.6 思考题 第3章 第一个Hibernate应用 3.1 创建Hibernate的配置文件 3.2 创建持久化类 3.3 创建数据库Schema 3.4 创建对象-关系映射...
2.4.3 Hibernate映射类型接口 2.4.4 可供扩展的接口 2.5 小结 2.6 思考题 第3章 第一个Hibernate应用 3.1 创建Hibernate的配置文件 3.2 创建持久化类 3.3 创建数据库Schema 3.4 创建对象-关系映射...
1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射Person类 1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 ...
2.4.3 Hibernate映射类型接口 2.4.4 可供扩展的接口 2.5 小结 2.6 思考题 第3章 第一个Hibernate应用 3.1 创建Hibernate的配置文件 3.2 创建持久化类 3.3 创建数据库Schema 3.4 创建对象-关系映射...
1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射Person类 1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 ...
1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射Person类 1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 ...
1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射Person类 1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 ...
实现一个默认的构造方法(constructor) 4.1.3. 提供一个标识属性(identifier property)(可选) 4.1.4. 建议使用不是final的类 (可选) 4.2. 实现继承(Inheritance) 4.3. 实现equals()和hashCode...
<br>mysql初始数据库用户名:root,密码:1234 <br>其它数据库请相应修改hibernate.cfg.xml这映射文件为与你数据库相匹配 <br>注释: 1,默认配置为mysql5.0 hibernate.cfg.xml.mssql为Ms sqlserver2000...
1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射Person类 1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 ...
两种配置文件: A.hibernate.cfg.xml 和 B.hibernate.properties A中可含映射文件的配置,而B中hard codes加映射文件。 A。Configuration config=new Configuration().config(); B. ...
2.2.2. 映射文件 2.2.3. Hibernate配置 2.2.4. 用Ant编译 2.2.5. 安装和帮助 2.2.6. 加载并存储对象 2.3. 第二部分 - 关联映射 2.3.1. 映射Person类 2.3.2. 一个单向的Set-based关联 2.3.3. 使关联工作 ...
4.1.2. 实现一个默认的(即无参数的)构造方法(constructor) 4.1.3. 提供一个标识属性(identifier property)(可选) 4.1.4. 使用非final的类 (可选) 4.2. 实现继承(Inheritance) 4.3. 实现equals()和...
1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射Person类 1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 ...
1.2.4 在映射文件配置HQL语句 15 1.3 HQL的嵌套子查询 15 1.3.1 嵌套子查询的概念 15 1.3.2 带有IN谓词的子查询 16 1.3.3 比较子查询 16 1.3.4 带有ANY或ALL的子查询 17 1.4 HQL的多表查询 17 1.4.1 表之间的关联...