生成的和默认的属性值
如果类的一个特定属性有着数据库生成的值,通常在第一次插入实体行的时候。典型的数据库生成的值是创建的时间戳 , 还有其它默认值等.
每当hibernate给定义了已生成或默认属性的实体执行sql insert或update时,它在插入默认值或生成值之后立即执行select。因为设置了generated=always,hibernate会在插入后自动的执行一个select,给java类中的属性设置,如果没有设置generated属性,需要显式调用session.flush()语句。这里就是说, 数据库默认生成值的字段,必须通过select后把值传给java实体的该属性。
使用property映射中的generated开关启用这个自动刷新:
标记为数据库生成的属性还必须是非可插入和非可更新的,用insert和update属性进行控制它们。如果两者都设置为false,属性的列就永远不会出现在insert或者update语句中---属性值是只读的。而且,通常不在类中给不可变的属性添加公有的设置方法(这时应切换到字段访问).
关于generated=""的适用值说明:
never(默认):标明此属性值不是从数据库中生成, 也就是根本不用刷新实体类了。
insert:标明此属性值在insert的时候生成,但是不会在随后的update时重新生成。也就是只在insert情况下才会刷新实体类。
always:标明此属性值在insert和update时都会被生成。也就是在insert,update情况下都会刷新实体类。
例1:
- package pojo;
- import java.io.serializable;
- import java.util.calendar;
- public class student implements serializable{
- private string id;
- private string name;
- //删除age的公共设置方法
- private int age;
- //删除createtime的公共设置方法
- private calendar createtime;
- //删除updatetime的公共设置方法
- private calendar updatetime;
-
- public string getid() {
- return id;
- }
- @suppresswarnings("unused")
- private void setid(string id) {
- this.id = id;
- }
- public string getname() {
- return name;
- }
- public void setname(string name) {
- this.name = name;
- }
- public int getage() {
- return age;
- }
- public calendar getcreatetime() {
- return createtime;
- }
- public calendar getupdatetime() {
- return updatetime;
- }
- }
- xml version="1.0" encoding="utf-8"?>
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="pojo">
- <class name="student" table="a_student">
- <id name="id" column="id" type="string">
- <generator class="uuid.hex"/>
- id>
- <property name="name" column="name" type="string"/>
-
-
-
-
- <property name="age" column="age" type="integer" insert="false" update="false"
- access="field" generated="always"/>
- <property name="createtime" column="createtime" type="calendar" insert="false"
- update="false" access="field" generated="always"/>
- <property name="updatetime" column="updatetime" type="calendar" insert="false"
- update="false" access="field" generated="always"/>
- class>
- hibernate-mapping>
例2:
- package pojo;
- import java.io.serializable;
- import java.util.calendar;
- public class student implements serializable{
- private string id;
- private string name;
- //删除age的公共设置方法
- private int age;
- //删除createtime的公共设置方法
- private calendar createtime;
- //删除updatetime的公共设置方法
- private calendar updatetime;
-
- public string getid() {
- return id;
- }
- @suppresswarnings("unused")
- private void setid(string id) {
- this.id = id;
- }
- public string getname() {
- return name;
- }
- public void setname(string name) {
- this.name = name;
- }
- public int getage() {
- return age;
- }
- public calendar getcreatetime() {
- return createtime;
- }
- public calendar getupdatetime() {
- return updatetime;
- }
- }
- xml version="1.0" encoding="utf-8"?>
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="pojo">
- <class name="student" table="a_student" dynamic-insert="true" dynamic-update="true">
- <id name="id" column="id" type="string">
- <generator class="uuid.hex"/>
- id>
- <property name="name" column="name" type="string"/>
-
-
-
-
- <property name="age" column="age" type="integer" access="field" generated="always"/>
- <property name="createtime" column="createtime" type="calendar" access="field" generated="always"/>
- <property name="updatetime" column="updatetime" type="calendar" access="field" generated="always"/>
- class>
- hibernate-mapping>
因为有了dynamic-insert="true" dynamic-update="true", 那么没有设值的属性(age,createtime,updatetime)就不会出现在insert,update的数据库执行语句中,所以也就没有必要在各属性上使用insert="false" update="false"。
例3,比较generated="always",generated="insert":
- package pojo;
- import java.io.serializable;
- import java.util.calendar;
- public class student implements serializable{
- private string id;
- private string name;
- //删除age的公共设置方法
- private int age;
- //删除createtime的公共设置方法
- private calendar createtime;
- //删除updatetime的公共设置方法
- private calendar updatetime;
-
- public string getid() {
- return id;
- }
- @suppresswarnings("unused")
- private void setid(string id) {
- this.id = id;
- }
- public string getname() {
- return name;
- }
- public void setname(string name) {
- this.name = name;
- }
- public int getage() {
- return age;
- }
- public calendar getcreatetime() {
- return createtime;
- }
- public calendar getupdatetime() {
- return updatetime;
- }
- }
- xml version="1.0" encoding="utf-8"?>
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="pojo">
- <class name="student" table="a_student" dynamic-insert="true" dynamic-update="true">
- <id name="id" column="id" type="string">
- <generator class="uuid.hex"/>
- id>
- <property name="name" column="name" type="string"/>
-
-
-
-
-
- <property name="age" column="age" type="integer" access="field" generated="insert"/>
- <property name="createtime" column="createtime" type="calendar" access="field" generated="always"/>
- <property name="updatetime" column="updatetime" type="calendar" access="field" generated="always"/>
- class>
- hibernate-mapping>
- package util;
- import org.hibernate.session;
- import org.hibernate.transaction;
- import pojo.student;
- public class manager {
- public static void main(string[] args) {
-
- student stu = new student();
- stu.setname("fuhaidong");
-
- session session = hibernateutil.getsessionfactory().opensession();
- transaction transaction = session.begintransaction();
-
- session.save(stu);
- student s = (student) session.get(student.class, "402881832b3dea91012b3dea925a0001");
- s.setname("dddd");
- transaction.commit();
- session.close();
-
- }
- }
输出日志:
- hibernate:
- select
- student0_.id as id0_0_,
- student0_.name as name0_0_,
- student0_.age as age0_0_,
- student0_.createtime as createtime0_0_,
- student0_.updatetime as updatetime0_0_
- from
- a_student student0_
- where
- student0_.id=?
- hibernate:
- insert
- into
- a_student
- (name, id)
- values
- (?, ?)
- hibernate:
- select
- student_.age as age0_,
- student_.createtime as createtime0_,
- student_.updatetime as updatetime0_
- from
- a_student student_
- where
- student_.id=?
- ------------上面插入数据后,三个列都被查询用来刷新实体了。
- hibernate:
- update
- a_student
- set
- name=?
- where
- id=?
- hibernate:
- select
- student_.createtime as createtime0_,
- student_.updatetime as updatetime0_
- from
- a_student student_
- where
- student_.id=?
- ------------更新数据后,只有声明为generated="always"的列被查询
最后要注意的是,数据库表的age,createtime,updatetime字段上都要有默认值,或者有触发器 ,不然上面所有例子的age,createtime,
updatetime在表中的值都是null.
描述的很通俗,所以转自:http://blog.csdn.net/fhd001/article/details/5878498
posted on 2013-02-28 09:55
紫蝶∏飛揚↗ 阅读(3994)
评论(0) 编辑 收藏 所属分类:
hibernate