当前位置:首页 >> 计算机软件及应用 >>

hibernate3 注解教程


Hibernate Annotation 笔记
(1)

简介:
在过去几年里, Hibernate 不断发展, 几乎成为 Java 数据库持久性的事实标准。 它非常强大、 灵活, 而且具备了优异的性能。 在本文中, 我们将了解如何使用 Java 5 注释来简化 Hibernate 代码,并使持久层的编码过程变得更为轻松。 传统上,Hibernate 的配置依赖于外部 XML 文件:数据库映射被定义为一组 XML 映射 文件,并且在启动时进行加载。 在最近发布的几个 Hibernate 版本中, 出现了一种基于 Java 5 注释的更为巧妙的新方法。 借 助新的 Hibernate Annotation 库, 即可一次性地分配所有旧映射文件——一切都会按照您的 想法来定义——注释直接嵌入到您的 Java 类中,并提供一种强大及灵活的方法来声明持久 性映射。 即利用 hibernate 注解后, 可不用定义持久化类对应的*.hbm.xml 文件, 直接以注解方式写入 在持久化类中来实现。 Hibernate annotation 使用了 ejb JPA 的注解,所以, 下面安装配置 hibernate annotation 环境时, 需要导入 ejb 的包。许多网上的资料都是 jpa hibernate annotation 方面的资料。 (2)

安装 Hibernate Annotation
第一步, 环境与 jar 包: 要使用 Hibernate Annotation ,您至少需要具备 Hibernate 3.2 和 Java 5 。可以从 Hibernate 站点下载 Hibernate 3.2 和 Hibernate Annotation 库。除了标准的 Hibernate JAR 和依赖项之外, 您还需要 Hibernate Annotations .jar 文件 (hibernate-annotations.jar) 、 Java 持 久性 API (lib/ejb3-persistence.jar) 。 添加 hibernate3.2.jar,hibernate-annotations- 3.3.0.jar,hibernate-commons-annotations.jar 和 ejb3-persistence.jar 。这样就可以使用 hibernate 的 annotation 了。 如果您正在使用 Maven,只需要向 POM 文件添加相应的依赖项即可,如下所示: ... <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate</artifactId> <version>3.2.1.ga</version> </dependency>

<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-annotations</artifactId> <version>3.2.0.ga</version> </dependency> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> </dependency> 第二步, 获取 Hibernate 会话工厂。尽管无需惊天的修改,但这一工作与使用 Hibernate Annotations 有所不同。您需要使用 AnnotationConfiguration 类来建立会话工厂: sessionFactory = new AnnotationConfiguration().buildSessionFactory(); 第三步, 尽管通常使用<mapping>元素来声明持久性类,您还是需要在 Hibernate 配置文件(通常是 hibernate.cfg.xml)中声明持久性类: <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <mapping class="com.onjava.modelplanes.domain.PlaneType"/> <mapping class="com.onjava.modelplanes.domain.ModelPlane"/> </session-factory> </hibernate-configuration> 近期的许多 Java 项目都使用了轻量级的应用框架,例如 Spring 。如果您正在使用 Spring 框架,可以使用 AnnotationSessionFactoryBean 类轻松建立一个基于注释的 Hibernate 会话工厂,如下所示: <!-- Hibernate session factory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource"/> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop> <prop key="hibernate.hbm2ddl.auto">create</prop> ... </props> </property> <property name="annotatedClasses"> <list>

<value>com.onjava.modelplanes.domain.PlaneType</value> <value>com.onjava.modelplanes.domain.ModelPlane</value> ... </list> </property> < /bean> (3)

hibernate Annotation 标签的使用:
[1]注解综述
1.带注释的持久性类也是普通 POJO,它们只是具备了持久性注释的普通 POJO 。 2.事实上,您既可以保持字段的持久性(注释写在成员变量之上) ,也可以保持属性(注释 写在 getter 方法之上)的持久性。 3.常用的 hibernate annotation 标签如下: @Entity --注释声明该类为持久类。将一个 Javabean 类声明为一个实体的数 据库表映射类,最好实现序列化.此时,默认情况下,所有的类属性都为映射到数据表的持久性 字段.若在类中,添加另外属性,而非映射来数据库的, 要用下面的 Transient 来注解.

@Table(name="promotion_info") --持久性映射的表(表名="promotion_info).@Table 是类 一级的注解,定义在@Entity 下,为实体 bean 映射表,目录和 schema 的名字,默认为实体 bean 的类名,不带包名.

@Id--注释可以表明哪种属性是该类中的独特标识符(即相当于数据表的主键)。 @GeneratedValue --定义自动增长的主键的生成策略. @Transient --将忽略这些字段和属性,不用持久化到数据库.适用于,在当前的持 久类中,某些属性不是用于映射到数据表,而是用于其它的业务逻辑需要,这时,须将这些属性 进行 transient 的注解.否则系统会因映射不到数据表相应字段而出错. @Temporal(TemporalType.TIMESTAMP)--声明时间格式 @Enumerated --声明枚举 @Version --声明添加对乐观锁定的支持 @OneToOne --可以建立实体 bean 之间的一对一的关联 @OneToMany --可以建立实体 bean 之间的一对多的关联 @ManyToOne --可以建立实体 bean 之间的多对一的关联 @ManyToMany --可以建立实体 bean 之间的多对多的关联 @Formula --一个 SQL 表达式, 这种属性是只读的,不在数据库生成属性(可以 使用 sum、average、max 等) @OrderBy --Many 端某个字段排序(List)

1.2 Hibernate 能够出色地自动生成主键。Hibernate/EBJ 3 注释也可以为主键的自动生成提供丰 富的支持,允许实现各种策略。 其生成规则由@GeneratedValue 设定的.这里的@id 和@GeneratedValue 都是 JPA 的标准用法, JPA 提供四种标准用法,由@GeneratedValue 的源代码可以明显看出. JPA 提供的四种标准用法为 TABLE,SEQUENCE,IDENTITY,AUTO. TABLE:使用一个特定的数据库表格来保存主键。 SEQUENCE:根据底层数据库的序列来生成主键,条件是数据库支持序列。 IDENTITY:主键由数据库自动生成(主要是自动增长型) AUTO:主键由程序控制。 在指定主键时,如果不指定主键生成策略,默认为 AUTO。 @Id 相当于 @Id @GeneratedValue(strategy = GenerationType.AUTO) identity: 使用 SQL Server 和 MySQL 的自增字段,这个方法不能放到 Oracle 中,Oracle 不支持自增 字段,要设定 sequence(MySQL 和 SQL Server 中很常用) 。 Oracle 就要采用 sequence 了. 同时,也可采用 uuid,native 等其它策略.(相关用法,上网查询)

[2]第一个持久类
第一个持久性类 @Entity @Table(name="T_MODEL_PLANE") public class ModelPlane implements Serializable { @Id @Column(name="PLANE_ID") @GeneratedValue(strategy=GenerationType.AUTO) //注解于属性中 /* 对于 oracle 想使用各自的 Sequence,设置如下: @GeneratedValue(strategy = GenerationType.AUTO,generator="PROMOTION_SEQ") @SequenceGenerator(name="PROMOTION_SEQ",sequenceName="PROMOTION_SEQ") 另外: 对于自动增长后,在数据表中的相应字段,要设置字段为 auto_increment. */ private Long id;

private String name;//注解写于 getter 方法之上.请见下.

//DATE - java.sql.Date //TIME - java.sql.Time //TIMESTAMP - java.sql.Timestamp @Temporal(TemporalType.TIMESTAMP) @Column(name="start_time") private Date startTime;

//显示 0 隐藏 1 public static enum DisplayType {显示,隐藏} @Enumerated(value = EnumType.ORDINAL)//ORDINAL 序数 private DisplayType displayType = DisplayType.显示;

//1.sql 语句中的字段和表名都应该和数据库相应,而不是类中的字段, //若带有参数如 la.id= id,这个=id 才是类中属性 //2.操作字段一定要用别名 @Formula(select COUNT(la.id) from largess la) private int count;

//注解于方法中 @Column(name="PLANE_ID", length=80, nullable=true) //较详细定义 public String getName() { return name; } public void setName(String name) { this.name = name; } 其它的 setter,getter 省略...... }

该内容将映射到下表中: CREATE TABLE T_MODEL_PLANE ( PLANE_ID long, PLANE_NAME varchar 其它字段省略... ) 默认情况下,Hibernate 会将持久类以匹配的名称映射到表和字段中。例如,下例中,若不 用注解,则会映射到如下一表中:

CREATE TABLE MODELPLANE ( ID long, NAME varchar 其它字段省略... )

[3]一对多注解
一对多注解: 1. 在一对多注解中,会用到: "一"方: @OneToMany --> mappedBy:"多"方的关联属性(被控方) "多"方: @ManyToOne --> @JoinColumn,"多"方定义的外键字段. 如数据表定义外键如下: FOREIGN KEY (classid) REFERENCES classes(id) 则: @JoinColumn(name="classid") 2. 在双向关联中, 有且仅有一端作为主体 (owner) 端存在: 主体端负责维护联接列 (即更新) , 对于不需要维护这种关系的从表则通过 mappedNy 属性进行声明。mappedBy 的值指向另一 主体的关联属性。例子中,mappedBy 的值为 classes。 附加说明: mappedBy 相当于过去的 inverse="true". inverse=false 的 side(side 其实是指 inverse=false 所位于的 class 元素)端有责任维护关系, 而 inverse=true 端无须维护这些关系。 3. cascade 与 fetch 使用说明: Cascade CascadeType.PERSIST (级联新建) CascadeType.REMOVE (级联删除) CascadeType.REFRESH (级联刷新) CascadeType.MERGE (级联更新)中选择一个或多个。 CascadeType.ALL fetch 属性: 关联关系获取方式,即是否采用延时加载。 LAZY(默认值)采用延时加载,查询数据时,不一起查询关联对象的数据。而是当访问关 联对象时(如:getStudnets()时)才触发相应的查询操作,获取关联对象数据。 EAGER:是在查询数据时,也直接一起获取关联对象的数据。 package oneToMany; import java.util.Set; import javax.persistence.*;

/* 注意导入时,是导入:import javax.persistence.*; 非导入 org.hibernate 的相关类:import org.hibernate.annotations.Entity; */ @Entity @Table(name="classes") public class Classes implements Serializable { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; private String name; @OneToMany(cascade=CascadeType.ALL,mappedBy="classes") private Set<Student> students; //getter,setter 省略 }

package oneToMany; import javax.persistence.*; @Entity @Table(name="student") public class Student implements Serializable { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int sid; private String sname; //若有多个 cascade,可以是:{CascadeType.PERSIST,CascadeType.MERGE} @ManyToOne(cascade={CascadeType.ALL}) @JoinColumn(name="classid") //student 类中对应外键的属性:classid private Classes classes; //getter,setter 省略 }

public class TestOneToMany { /* CREATE TABLE student (

--要定义外键!!!!!!!

`sid` double NOT NULL auto_increment, `classid` double NULL, `sname` varchar(255) NOT NULL, PRIMARY KEY (sid), INDEX par_ind (classid), FOREIGN KEY (classid) REFERENCES classes(id) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB */ public static void main(String[] args) throws SQLException { try { SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory(); Session session=sf.openSession(); Transaction tx=session.beginTransaction(); /* 因为 mappedBy 是定义在 classes 中,即 classes 类不负责维护级联关系.即维护者是 student.所 以, 1.要将 clsses 的数据,赋给 student,即用 student 的 setClasses()方法去捆定 class 数据; 2.在进行数据插入/更新 session.save()/session.update()时,最后操作的是 student. */ Classes classes=new Classes(); classes.setName("access"); Student st1=new Student(); st1.setSname("jason"); st1.setClasses(classes); session.save(st1); Student st2=new Student(); st2.setSname("hwj"); st2.setClasses(classes); session.save(st2); tx.commit(); /* 输出如下: Hibernate: insert into classes (name) values (?) Hibernate: insert into student (classid, sname) values (?, ?) Hibernate: insert into student (classid, sname) values (?, ?) */ /* 因为一端维护关系另一端不维护关系的原因, 我们必须注意避免在应用中用不维护关系的类 (class)建立关系,因为这样建立的关系是不会在数据库中存储的。 如上的代码倒过来,则插入时,student 的外键值为空.如下:

*/ // // // // // // // // // // // // // // // // /*

Student st1=new Student(); st1.setSname("jason"); session.save(st1); Student st2=new Student(); st2.setSname("hwj"); session.save(st2); Set<Student> students=new HashSet<Student>(); students.add(st1); students.add(st2); Classes classes=new Classes(); classes.setName("access"); classes.setStudents(students); session.save(classes);

输出如下: Hibernate: insert into student (classid, sname) values (?, ?) Hibernate: insert into student (classid, sname) values (?, ?) Hibernate: insert into classes (name) values (?) */ } catch(HibernateException e) { e.printStackTrace(); } } }

[4]多对多注解
多对多注解: 在多对多注解中,双方都采用@ManyToMany. 其中被控方,像一对多注解中设置一样,也要设置 mappedBy. 其中主控方,不像一对多注解那样,采用@joinColumn,而是采用@joinTable.如下: @JoinTable(name="j_student_course" ,joinColumns={@JoinColumn(name="sid")},inverseJoinCol umns={@JoinColumn(name="cid")}) 其中, 如上所说,mappedBy,相当于 inverse="true".所以,在@joinTable 中的 inverseJoinColumns 中定义 的字段为 mappedBy 所在类的主键.

joinColumns 定义的字段,就是当前类的主键. @Entity @Table(name="jcourse") public class Jcourse { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int cid; private String cname; @ManyToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE},fetch=FetchType.LAZY ,ma ppedBy="courses") private Set<Jstudent> students; //setter,getter 省略.... }

@Entity @Table(name="jstudent") public class Jstudent { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int sid; private String sname; @ManyToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE},fetch=FetchType.EAGER) //inverseJoinColumns 中对应的 id 为以下属性 course 的对应 id. @JoinTable(name="j_student_course" ,joinColumns={@JoinColumn(name="sid")},inverseJoinCol umns={@JoinColumn(name="cid")}) private Set<Jcourse> courses; //setter,getter 省略.... }

public class Test { public static void main(String[] args) { try { SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory(); Session session=sf.openSession();

Transaction tx=session.beginTransaction(); Jcourse course=new Jcourse(); course.setCname("jason-english"); session.save(course); //先各自保存. Jcourse course2=new Jcourse(); course2.setCname("herry-english"); session.save(course2); Set<Jcourse> courses=new HashSet<Jcourse>(); courses.add(course); courses.add(course2); Jstudent student=new Jstudent(); student.setSname("jason"); student.setCourses(courses); session.save(student);// 要用非 mapby 定义的类(studet)来作为主者(会控制级联关系), 一对多,多对一也一样道理. //可以尝试反过来. tx.commit(); } catch(HibernateException e) { e.printStackTrace(); } } }

级联操作
级联保存
jpa 级联操作详解 1-级联保存(CascadeType.PERSIST)

不论是对于 jpa 或者是 hibernate 来说字段的数据库映射都不是难点,而是很多初学者都对 jpa 级联操作等一系列的东西不大明白,在这一系列的文章中我通过简单的 java 实体对象来 与大家共同理解 jpa(hibernate 做实现产品)的级联操作等难点知识,希望能够共同提高。 为了保证简单易懂,本系列文章避免光讲理论知识,而忽视实际动手,在下面的例子中都有 简单易懂的例子, 为了加深理解大家也可以在自己的机器上调试。 同时为了方便理解本系列 文章采用对比讲解,能让人一目了然。同时欢迎大家共同探讨,一起完善这教程

jpa 级联操作详解 1(cascade) 之 cascade={CascadeType.PERSIST}

onetomany 一对多关联实体 bean:汽车和车库

(一) Java 代码

1. package com.hibernate.jpa.bean1; 2. 3. import javax.persistence.Entity; 4. import javax.persistence.GeneratedValue; 5. import javax.persistence.Id; 6. import javax.persistence.JoinColumn; 7. import javax.persistence.ManyToOne; 8. @Entity 9. public class Auto { 10. 11. /** 12. * one to many 一对多关联 13. */ 14. private Integer autoId; 15. private String autotype; 16. private String autonum; 17. private Garage garage;

18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. }

@Id @GeneratedValue public Integer getAutoId() { return autoId; } public void setAutoId(Integer autoId) { this.autoId = autoId; } public String getAutotype() { return autotype; } public void setAutotype(String autotype) { this.autotype = autotype; } public String getAutonum() { return autonum; } public void setAutonum(String autonum) { this.autonum = autonum; } @ManyToOne @JoinColumn(name="garageid") public Garage getGarage() { return garage; } public void setGarage(Garage garage) { this.garage = garage; }

48. ------车库 49. package com.hibernate.jpa.bean1; 50. 51. import java.util.HashSet; 52. import java.util.Set; 53. 54. import javax.persistence.Column; 55. import javax.persistence.Entity; 56. import javax.persistence.GeneratedValue; 57. import javax.persistence.Id; 58. import javax.persistence.OneToMany; 59. @Entity 60. public class Garage { 61.

62.

/**

63. * many to one 多对一 64. */ 65. private Integer gid; 66. private String garagenum; 67. private Set<Auto> autos = new HashSet<Auto>(); 68. 69. @Id @GeneratedValue 70. public Integer getGid() { 71. return gid; 72. } 73. public void setGid(Integer gid) { 74. this.gid = gid; 75. } 76. @Column(length=20) 77. public String getGaragenum() { 78. return garagenum; 79. } 80. public void setGaragenum(String garagenum) { 81. this.garagenum = garagenum; 82. } 83. @OneToMany(mappedBy="garage") 84. public Set<Auto> getAutos() { 85. return autos; 86. } 87. public void setAutos(Set<Auto> autos) { 88. this.autos = autos; 89. } 90. public void addGarageAuto(Auto auto) { 91. auto.setGarage(this); 92. this.autos.add(auto); 93. } 94. 95. } 96. ---------junit 保存方法 97. @Test public void save() { 98. EntityManagerFactory Persistence.createEntityManagerFactory("jpa-hibernate"); 99. EntityManager em = factory.createEntityManager(); 100. em.getTransaction().begin(); 101. 102. Garage garage = new Garage(); 103. garage.setGaragenum("room1"); 104.

factory

=

105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120.

Auto auto1 = new Auto(); auto1.setAutonum("bj0000"); auto1.setAutotype("car"); Auto auto2 = new Auto(); auto2.setAutonum("bj1231"); auto2.setAutotype("bus"); garage.addGarageAuto(auto1); garage.addGarageAuto(auto2); em.persist(garage); em.getTransaction().commit(); em.close(); factory.close(); }

package com.hibernate.jpa.bean1;

import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; @Entity public class Auto {

/** * one to many 一对多关联 */ private Integer autoId; private String autotype; private String autonum; private Garage garage;

@Id @GeneratedValue public Integer getAutoId() { return autoId; } public void setAutoId(Integer autoId) {

this.autoId = autoId; } public String getAutotype() { return autotype; } public void setAutotype(String autotype) { this.autotype = autotype; } public String getAutonum() { return autonum; } public void setAutonum(String autonum) { this.autonum = autonum; } @ManyToOne @JoinColumn(name="garageid") public Garage getGarage() { return garage; } public void setGarage(Garage garage) { this.garage = garage; }

} ------车库 package com.hibernate.jpa.bean1;

import java.util.HashSet; import java.util.Set;

import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; @Entity public class Garage {

/** * many to one 多对一

*/ private Integer gid; private String garagenum; private Set<Auto> autos = new HashSet<Auto>(); @Id @GeneratedValue public Integer getGid() { return gid; } public void setGid(Integer gid) { this.gid = gid; } @Column(length=20) public String getGaragenum() { return garagenum; } public void setGaragenum(String garagenum) { this.garagenum = garagenum; } @OneToMany(mappedBy="garage") public Set<Auto> getAutos() { return autos; } public void setAutos(Set<Auto> autos) { this.autos = autos; } public void addGarageAuto(Auto auto) { auto.setGarage(this); this.autos.add(auto); }

} ---------junit 保存方法 @Test public void save() { EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa-hibernate"); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); Garage garage = new Garage(); garage.setGaragenum("room1"); Auto auto1 = new Auto(); auto1.setAutonum("bj0000");

auto1.setAutotype("car"); Auto auto2 = new Auto(); auto2.setAutonum("bj1231"); auto2.setAutotype("bus"); garage.addGarageAuto(auto1); garage.addGarageAuto(auto2); em.persist(garage); em.getTransaction().commit(); em.close(); factory.close(); }

运行以上 save()方法之后,数据库中只对应的表,但是只有 garage 表中被存入了数据,而 auto 表中没有被存入数据,仅仅是生成了表而已。

数据库中的表数据:

+-----+-----------+ | gid | garagenum | +-----+-----------+ | 1 | room1 | +-----+-----------+

mysql> select * from auto; Empty set (0.00 sec)

这儿可以注意到虽然生成了 auto 数据库表,但是无法存储有关 auto 的数据,因为没有先保 存 auto 或设置级联保存

观察发出的 sql 语句:

Hibernate: insert into Garage (garagenum) values (?)

(二)先保存 auto

将 junit 测试类中的 save 方法改为

@Test public void save() { EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa-hibernate"); EntityManager em = factory.createEntityManager(); em.getTransaction().begin();

Garage garage = new Garage(); garage.setGaragenum("room1");

Auto auto1 = new Auto(); auto1.setAutonum("bj0000"); auto1.setAutotype("car");

Auto auto2 = new Auto(); auto2.setAutonum("bj1231"); auto2.setAutotype("bus");

garage.addGarageAuto(auto1); garage.addGarageAuto(auto2); em.persist(auto1); em.persist(auto2); em.persist(garage); em.getTransaction().commit(); em.close(); factory.close(); }

观察发出的 sql 语句:

Hibernate: insert into Auto (autonum, autotype, garageid) values (?, ?, ?) Hibernate: insert into Auto (autonum, autotype, garageid) values (?, ?, ?) Hibernate: insert into Garage (garagenum) values (?) Hibernate: update Auto set autonum=?, autotype=?, garageid=? where autoId=? Hibernate: update Auto set autonum=?, autotype=?, garageid=? where autoId=?

当然也生成了对应的数据记录,但是对数据库进行了 5 次操作

mysql> select * from garage; +-----+-----------+ | gid | garagenum | +-----+-----------+ | 1 | room1 | +-----+-----------+

mysql> select * from auto; +--------+---------+----------+----------+ | autoId | autonum | autotype | garageid | +--------+---------+----------+----------+ | 1 | bj0000 | car | 1 | | 2 | bj1231 | bus | 1 | +--------+---------+----------+----------+

-----------------------------------------------------------------------

(三)设置 cascade={CascadeType.PERSIST}

当把

@OneToMany(mappedBy="garage") public Set<Auto> getAutos() { return autos; }

改为:

@OneToMany(cascade={CascadeType.PERSIST} ,mappedBy="garage") public Set<Auto> getAutos() { return autos; }

即多添加了一行 cascade={CascadeType.PERSIST} 申明级联级联保存

删除前面生成的数据库表 garage 和 auto

再次运行 save()方法

这是我们看到数据库中都有对应的记录

+-----+-----------+ | gid | garagenum | +-----+-----------+ | 1 | room1 | +-----+-----------+

+--------+---------+----------+----------+ | autoId | autonum | autotype | garageid | +--------+---------+----------+----------+ | 1 | bj0000 | car | 1 |

| 2 | bj1231 | bus | 1 | +--------+---------+----------+----------+

观察发出的 sql 语句:

Hibernate: insert into Garage (garagenum) values (?) Hibernate: insert into Auto (autonum, autotype, garageid) values (?, ?, ?) Hibernate: insert into Auto (autonum, autotype, garageid) values (?, ?, ?)

级联删除
jpa 级联操作详解 2--级联删除(CascadeType.REMOVE)

aad

Garage.java Java 代码

1. package com.hibernate.jpa.bean1; 2. 3. import java.util.HashSet; 4. import java.util.Set; 5. 6. import javax.persistence.CascadeType; 7. import javax.persistence.Column; 8. import javax.persistence.Entity; 9. import javax.persistence.FetchType; 10. import javax.persistence.GeneratedValue; 11. import javax.persistence.Id; 12. import javax.persistence.OneToMany; 13. @Entity 14. public class Garage {

15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. }

/** * many to one 多对一 */ private Integer gid; private String garagenum; private Set<Auto> autos = new HashSet<Auto>(); @Id @GeneratedValue public Integer getGid() { return gid; } public void setGid(Integer gid) { this.gid = gid; } @Column(length=20) public String getGaragenum() { return garagenum; } public void setGaragenum(String garagenum) { this.garagenum = garagenum; } @OneToMany(cascade={CascadeType.PERSIST},mappedBy="garage") public Set<Auto> getAutos() { return autos; } public void setAutos(Set<Auto> autos) { this.autos = autos; } public void addGarageAuto(Auto auto) { auto.setGarage(this); this.autos.add(auto); }

package com.hibernate.jpa.bean1;

import java.util.HashSet; import java.util.Set;

import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; @Entity public class Garage {

/** * many to one 多对一 */ private Integer gid; private String garagenum; private Set<Auto> autos = new HashSet<Auto>(); @Id @GeneratedValue public Integer getGid() { return gid; } public void setGid(Integer gid) { this.gid = gid; } @Column(length=20) public String getGaragenum() { return garagenum; } public void setGaragenum(String garagenum) { this.garagenum = garagenum; } @OneToMany(cascade={CascadeType.PERSIST},mappedBy="garage") public Set<Auto> getAutos() { return autos; } public void setAutos(Set<Auto> autos) { this.autos = autos; } public void addGarageAuto(Auto auto) { auto.setGarage(this); this.autos.add(auto); }

}

Auto.java Java 代码

1. package com.hibernate.jpa.bean1; 2. 3. import javax.persistence.CascadeType; 4. import javax.persistence.Entity; 5. import javax.persistence.GeneratedValue; 6. import javax.persistence.Id; 7. import javax.persistence.JoinColumn; 8. import javax.persistence.ManyToOne; 9. @Entity 10. public class Auto { 11. 12. /** 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. * one to many 一对多关联 */ private Integer autoId; private String autotype; private String autonum; private Garage garage; @Id @GeneratedValue public Integer getAutoId() { return autoId; } public void setAutoId(Integer autoId) { this.autoId = autoId; } public String getAutotype() { return autotype; } public void setAutotype(String autotype) { this.autotype = autotype; } public String getAutonum() { return autonum; } public void setAutonum(String autonum) {

37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. }

this.autonum = autonum; } @ManyToOne() @JoinColumn(name="garageid") public Garage getGarage() { return garage; } public void setGarage(Garage garage) { this.garage = garage; }

package com.hibernate.jpa.bean1;

import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; @Entity public class Auto {

/** * one to many 一对多关联 */ private Integer autoId; private String autotype; private String autonum; private Garage garage;

@Id @GeneratedValue public Integer getAutoId() { return autoId; } public void setAutoId(Integer autoId) { this.autoId = autoId; } public String getAutotype() {

return autotype; } public void setAutotype(String autotype) { this.autotype = autotype; } public String getAutonum() { return autonum; } public void setAutonum(String autonum) { this.autonum = autonum; } @ManyToOne() @JoinColumn(name="garageid") public Garage getGarage() { return garage; } public void setGarage(Garage garage) { this.garage = garage; }

}

持久化数据

mysql> select * from garage; +-----+-----------+ | gid | garagenum | +-----+-----------+ | 1 | room1 | | 2 | room2 | | 3 | room3 | +-----+-----------+

mysql> select * from auto; +--------+---------+----------+----------+ | autoId | autonum | autotype | garageid | +--------+---------+----------+----------+

| 1 | hk2222 | car | 1 | | 2 | bj0000 | car | 1 | | 3 | jn1d31 | bus | 3 | | 4 | sh3243 | car | 3 | +--------+---------+----------+----------+

junit 测试方法 delete() Java 代码

1. @Test public void delete() { 2. EntityManagerFactory Persistence.createEntityManagerFactory("jpa-hibernate"); 3. EntityManager em = factory.createEntityManager(); 4. em.getTransaction().begin(); 5. Garage garage = em.find(Garage.class, 3); 6. em.remove(garage); 7. em.getTransaction().commit(); 8. em.close(); 9. factory.close(); 10. }

factory

=

@Test public void delete() { EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa-hibernate"); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); Garage garage = em.find(Garage.class, 3); em.remove(garage); em.getTransaction().commit(); em.close(); factory.close(); }

调用 delete 方法是 myeclipse 控制台出现异常

javax.persistence.RollbackException: Error while commiting the transaction

Caused by: java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`itcast/auto`, CONSTRAINT `FK1F51CFA8A25FB2` FOREIGN KEY (`garageid`) REFERENCES `garage` (`gid`))

发出的 sql 语句是: C#代码

1. Hibernate: select garage0_.gid as gid1_0_, garage0_.garagenum as garagenum1_0_ from Garage garage0_ where garage0_.gid=?

Hibernate: select garage0_.gid as gid1_0_, garage0_.garagenum as garagenum1_0_ from Garage garage0_ where garage0_.gid=?

(二)在 Garage.java 中添加 CascadeType.REMOVE 注解 Java 代码

1. @OneToMany(cascade={CascadeType.PERSIST,CascadeType.REMOVE},mappedBy="garage") 2. public Set<Auto> getAutos() { 3. return autos; 4. }

@OneToMany(cascade={CascadeType.PERSIST,CascadeType.REMOVE},mappedBy="garage") public Set<Auto> getAutos() { return autos; }

此时再次调用 junit 单元测试的 delete 方法

测试显示成功,发出的 sql 语句为 Sql 代码

1. Hibernate: select garage0_.gid as gid1_0_, garage0_.garagenum as garagenum1_0_ from Garage garage0_ where garage0_.gid=? 2. Hibernate: select autos0_.garageid as garageid1_, autos0_.autoId as autoId1_, autos0_.autoId as autoId0_0_, autos0_.autonum as autonum0_0_, autos0_.autotype as autotype0_0_, autos0_.garageid as garageid0_0_ from Auto autos0_ where autos0_.garageid=? 3. Hibernate: delete from Auto where autoId=? 4. Hibernate: delete from Auto where autoId=? 5. Hibernate: delete from Garage where gid=?

Hibernate: select garage0_.gid as gid1_0_, garage0_.garagenum as garagenum1_0_ from Garage garage0_ where garage0_.gid=? Hibernate: select autos0_.garageid as garageid1_, autos0_.autoId as autoId1_, autos0_.autoId as autoId0_0_, autos0_.autonum as autonum0_0_, autos0_.autotype as autotype0_0_, autos0_.garageid as garageid0_0_ from Auto autos0_ where autos0_.garageid=? Hibernate: delete from Auto where autoId=? Hibernate: delete from Auto where autoId=? Hibernate: delete from Garage where gid=?

此时表 garage 中的 gid 为 3 的字段被全部删除,同时也级联删除了与 garage 相关联表 auto 中 garageid 为 3 的字段 Sql 代码

1. mysql> select * from garage; 2. +-----+-----------+ 3. | gid | garagenum | 4. +-----+-----------+ 5. | 1 | room1 | 6. | 2 | room2 |

7. +-----+-----------+

mysql> select * from garage; +-----+-----------+ | gid | garagenum | +-----+-----------+ | 1 | room1 | | 2 | room2 | +-----+-----------+

Sql 代码

1. mysql> select * from auto; 2. +--------+---------+----------+----------+ 3. | autoId | autonum | autotype | garageid | 4. +--------+---------+----------+----------+ 5. | 1 | hk2222 | car | 1| 6. | 2 | bj0000 | car | 1| 7. +--------+---------+----------+----------+

mysql> select * from auto; +--------+---------+----------+----------+ | autoId | autonum | autotype | garageid | +--------+---------+----------+----------+ | 1 | hk2222 | car | 1| | 2 | bj0000 | car | 1| +--------+---------+----------+----------+

怎么样,这下级联删除也明白了吧?

级联抓取
jpa 抓取策略详解 fetch(lazy ,eager) 文章分类:数据库

在 jpa 中 jpa 默认的加载方式是 lazy 方式也就是在实际使用到数据的时候才加载相关数据, 使用 lazy 时可以不用显示注明 fetch=FetchType.LAZY

实体 bean:carage Java 代码

1. package com.hibernate.jpa.bean1; 2. 3. import java.util.HashSet; 4. import java.util.Set; 5. 6. import javax.persistence.CascadeType; 7. import javax.persistence.Column; 8. import javax.persistence.Entity; 9. import javax.persistence.FetchType; 10. import javax.persistence.GeneratedValue; 11. import javax.persistence.Id; 12. import javax.persistence.OneToMany; 13. @Entity 14. public class Garage { 15. 16. /** 17. * many to one 多对一 18. */ 19. private Integer gid; 20. private String garagenum; 21. private Set<Auto> autos = new HashSet<Auto>(); 22. 23. @Id @GeneratedValue 24. public Integer getGid() { 25. return gid; 26. } 27. public void setGid(Integer gid) { 28. this.gid = gid;

29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. }

} @Column(length=20) public String getGaragenum() { return garagenum; } public void setGaragenum(String garagenum) { this.garagenum = garagenum; } @OneToMany(cascade={CascadeType.PERSIST},mappedBy="garage") public Set<Auto> getAutos() { return autos; } public void setAutos(Set<Auto> autos) { this.autos = autos; } public void addGarageAuto(Auto auto) { auto.setGarage(this); this.autos.add(auto); }

package com.hibernate.jpa.bean1;

import java.util.HashSet; import java.util.Set;

import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; @Entity public class Garage {

/** * many to one 多对一 */

private Integer gid; private String garagenum; private Set<Auto> autos = new HashSet<Auto>(); @Id @GeneratedValue public Integer getGid() { return gid; } public void setGid(Integer gid) { this.gid = gid; } @Column(length=20) public String getGaragenum() { return garagenum; } public void setGaragenum(String garagenum) { this.garagenum = garagenum; } @OneToMany(cascade={CascadeType.PERSIST},mappedBy="garage") public Set<Auto> getAutos() { return autos; } public void setAutos(Set<Auto> autos) { this.autos = autos; } public void addGarageAuto(Auto auto) { auto.setGarage(this); this.autos.add(auto); }

}

实体 bean:auto Java 代码

1. package com.hibernate.jpa.bean1; 2. 3. import javax.persistence.CascadeType; 4. import javax.persistence.Entity; 5. import javax.persistence.GeneratedValue; 6. import javax.persistence.Id;

7. import javax.persistence.JoinColumn; 8. import javax.persistence.ManyToOne; 9. @Entity 10. public class Auto { 11. 12. /** 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. } * one to many 一对多关联 */ private Integer autoId; private String autotype; private String autonum; private Garage garage; @Id @GeneratedValue public Integer getAutoId() { return autoId; } public void setAutoId(Integer autoId) { this.autoId = autoId; } public String getAutotype() { return autotype; } public void setAutotype(String autotype) { this.autotype = autotype; } public String getAutonum() { return autonum; } public void setAutonum(String autonum) { this.autonum = autonum; } @ManyToOne() @JoinColumn(name="garageid") public Garage getGarage() { return garage; } public void setGarage(Garage garage) { this.garage = garage; }

package com.hibernate.jpa.bean1;

import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; @Entity public class Auto {

/** * one to many 一对多关联 */ private Integer autoId; private String autotype; private String autonum; private Garage garage;

@Id @GeneratedValue public Integer getAutoId() { return autoId; } public void setAutoId(Integer autoId) { this.autoId = autoId; } public String getAutotype() { return autotype; } public void setAutotype(String autotype) { this.autotype = autotype; } public String getAutonum() { return autonum; } public void setAutonum(String autonum) { this.autonum = autonum; } @ManyToOne() @JoinColumn(name="garageid") public Garage getGarage() {

return garage; } public void setGarage(Garage garage) { this.garage = garage; }

}

junit 的测试方法 Java 代码

1. @Test public void query() { 2. EntityManagerFactory Persistence.createEntityManagerFactory("jpa-hibernate"); 3. EntityManager em = factory.createEntityManager(); 4. 5. Garage garage = em.find(Garage.class, 1); 6. 7. 8. em.close(); 9. factory.close(); 10. }

factory

=

@Test public void query() { EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa-hibernate"); EntityManager em = factory.createEntityManager(); Garage garage = em.find(Garage.class, 1);

em.close(); factory.close(); }

调用 query 方法的时候发出的 sql 语句是: Sql 代码

1. Hibernate: select garage0_.gid as gid1_0_, garage0_.garagenum as garagenum1_0_ from Garage garage0_ where garage0_.gid=?

Hibernate: select garage0_.gid as gid1_0_, garage0_.garagenum as garagenum1_0_ from Garage garage0_ where garage0_.gid=?

也就是仅仅获取了 garage 对象,而没有获取与 garage 关联的 auto 对象

-----------------

(二)在 Garage.java 中添加 fetch=FetchType.EAGER 字段

@OneToMany(cascade={CascadeType.PERSIST},fetch=FetchType.EAGER,mappedBy="garage") public Set<Auto> getAutos() { return autos; }

再次运行 query 方法,这一次发出的 sql 语句是: Sql 代码

1. Hibernate: select garage0_.gid as gid1_1_, garage0_.garagenum as garagenum1_1_, autos1_.garageid as garageid3_, autos1_.autoId as autoId3_, autos1_.autoId as autoId0_0_, autos1_.autonum as autonum0_0_, autos1_.autotype as autotype0_0_, autos1_.garageid as garageid0_0_ from Garage garage0_ left outer join Auto autos1_ on garage0_.gid=autos1_.garageid where garage0_.gid=?

Hibernate: select garage0_.gid as gid1_1_, garage0_.garagenum as garagenum1_1_, autos1_.garageid as garageid3_, autos1_.autoId as autoId3_, autos1_.autoId as autoId0_0_, autos1_.autonum as autonum0_0_, autos1_.autotype as autotype0_0_, autos1_.garageid as garageid0_0_ from Garage garage0_ left outer join Auto autos1_ on garage0_.gid=autos1_.garageid where garage0_.gid=?

这一次由于将 jpa 默认的抓取策略改为 fetch=FetchType.EAGER

所以 jpa 在加载数据的时候一次性的加载了和 garage 相关联的数据

级联更新
jpa 级联操作详解 4-级联更新(CascadeType.MERGE) 文章分类:Java 编程

在 jpa 的应用中级联更新相比其他的不是很常用,但是也很有了解的必要

在这一讲的例子中我们依然以车库和汽车做实体类

Garage.java Java 代码

1. package com.hibernate.jpa.bean1; 2. 3. import java.util.HashSet; 4. import java.util.Set; 5. 6. import javax.persistence.CascadeType; 7. import javax.persistence.Column; 8. import javax.persistence.Entity; 9. import javax.persistence.FetchType; 10. import javax.persistence.GeneratedValue; 11. import javax.persistence.Id; 12. import javax.persistence.OneToMany; 13. @Entity

14. public class Garage { 15. 16. /** 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. } * many to one jpa 多对一 hibernate 实现 */ private Integer gid; private String garagenum; private Set<Auto> autos = new HashSet<Auto>(); @Id @GeneratedValue public Integer getGid() { return gid; } public void setGid(Integer gid) { this.gid = gid; } @Column(length=20) public String getGaragenum() { return garagenum; } public void setGaragenum(String garagenum) { this.garagenum = garagenum; } @OneToMany(cascade={CascadeType.PERSIST},mappedBy="garage") public Set<Auto> getAutos() { return autos; } // CascadeType.PERSIST:级联保存 public void setAutos(Set<Auto> autos) { this.autos = autos; } public void addGarageAuto(Auto auto) { auto.setGarage(this); this.autos.add(auto); }

package com.hibernate.jpa.bean1;

import java.util.HashSet; import java.util.Set;

import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; @Entity public class Garage {

/** * many to one jpa 多对一 hibernate 实现 */ private Integer gid; private String garagenum; private Set<Auto> autos = new HashSet<Auto>(); @Id @GeneratedValue public Integer getGid() { return gid; } public void setGid(Integer gid) { this.gid = gid; } @Column(length=20) public String getGaragenum() { return garagenum; } public void setGaragenum(String garagenum) { this.garagenum = garagenum; } @OneToMany(cascade={CascadeType.PERSIST},mappedBy="garage") public Set<Auto> getAutos() { return autos; } // CascadeType.PERSIST:级联保存 public void setAutos(Set<Auto> autos) { this.autos = autos; } public void addGarageAuto(Auto auto) { auto.setGarage(this);

this.autos.add(auto); }

}

Auto.java Java 代码

1. package com.hibernate.jpa.bean1; 2. 3. import javax.persistence.CascadeType; 4. import javax.persistence.Entity; 5. import javax.persistence.GeneratedValue; 6. import javax.persistence.Id; 7. import javax.persistence.JoinColumn; 8. import javax.persistence.ManyToOne; 9. @Entity 10. public class Auto { 11. 12. /** 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. * one to many jpa 一对多关联 hibernate 实现 */ private Integer autoId; private String autotype; private String autonum; private Garage garage; @Id @GeneratedValue public Integer getAutoId() { return autoId; } public void setAutoId(Integer autoId) { this.autoId = autoId; } public String getAutotype() { return autotype; } public void setAutotype(String autotype) { this.autotype = autotype; } public String getAutonum() {

34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. }

return autonum; } public void setAutonum(String autonum) { this.autonum = autonum; } @ManyToOne(cascade={CascadeType.REMOVE,CascadeType.REFRESH}) @JoinColumn(name="garageid") public Garage getGarage() { return garage; } // CascadeType.REMOVE:级联删除 ; CascadeType.REFRESH:级联刷新 public void setGarage(Garage garage) { this.garage = garage; }

package com.hibernate.jpa.bean1;

import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; @Entity public class Auto {

/** * one to many jpa 一对多关联 hibernate 实现 */ private Integer autoId; private String autotype; private String autonum; private Garage garage;

@Id @GeneratedValue public Integer getAutoId() { return autoId; }

public void setAutoId(Integer autoId) { this.autoId = autoId; } public String getAutotype() { return autotype; } public void setAutotype(String autotype) { this.autotype = autotype; } public String getAutonum() { return autonum; } public void setAutonum(String autonum) { this.autonum = autonum; } @ManyToOne(cascade={CascadeType.REMOVE,CascadeType.REFRESH}) @JoinColumn(name="garageid") public Garage getGarage() { return garage; } // CascadeType.REMOVE:级联删除 ; CascadeType.REFRESH:级联刷新 public void setGarage(Garage garage) { this.garage = garage; }

}

单元测试方法 Java 代码

1. @Test public void update() { 2. EntityManagerFactory Persistence.createEntityManagerFactory("jpa-hibernate"); 3. EntityManager em = factory.createEntityManager(); 4. em.getTransaction().begin(); 5. Garage garage = em.find(Garage.class, 1); 6. garage.setGaragenum("RoomAA"); 7. 8. em.getTransaction().commit(); 9. em.close(); 10. factory.close();

factory

=

11. }

@Test public void update() { EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa-hibernate"); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); Garage garage = em.find(Garage.class, 1); garage.setGaragenum("RoomAA"); em.getTransaction().commit(); em.close(); factory.close(); }

运行观察 Sql 代码

1. Hibernate: select garage0_.gid as gid1_0_, garage0_.garagenum as garagenum1_0_ from Garage garage0_ where garage0_.gid=? 2. Hibernate: update Garage set garagenum=? where gid=?

Hibernate: select garage0_.gid as gid1_0_, garage0_.garagenum as garagenum1_0_ from Garage garage0_ where garage0_.gid=? Hibernate: update Garage set garagenum=? where gid=?

(二)添加 CascadeType.MERGE 注解 Java 代码

//// CascadeType.MERGE 级联合并

1. @OneToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE},mappedBy="garage") 2. public Set<Auto> getAutos() { 3. return autos; 4. }

@OneToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE},mappedBy="garage")

public Set<Auto> getAutos() { return autos; }

将单元测试方法中的 Java 代码

1. garage.setGaragenum("RoomAA");

garage.setGaragenum("RoomAA");

改一下 Java 代码

1. garage.setGaragenum("RoomBB");

garage.setGaragenum("RoomBB");

运行 update() Sql 代码

1. Hibernate: select garage0_.gid as gid1_0_, garage0_.garagenum as garagenum1_0_ from Garage garage0_ where garage0_.gid=? 2. Hibernate: update Garage set garagenum=? where gid=?

Hibernate: select garage0_.gid as gid1_0_, garage0_.garagenum as garagenum1_0_ from Garage garage0_ where garage0_.gid=? Hibernate: update Garage set garagenum=? where gid=?

可见,这与上次发出的 sql 语句是一样的


相关文章:
Hibernate+JPA注解教程
Hibernate+JPA注解教程_理学_高等教育_教育专区。Hibernate+JPA注解教程Hibernate+JPA 注解教程一、开发环境 ? MyEclipse 8.5。因为 JPA 只能应用在 Hibernate3.2 及...
Hibernate注解详解(超全面不解释)
Hibernate注解详解(超全面不解释)_计算机软件及应用_IT/计算机_专业资料。...@ManyToOne public Presenter presenter; } 映射继承关系 EJB 支持3种类型的继承...
Hibernate_Annotation中文教程
Hibernate_Annotation中文教程_IT/计算机_专业资料。一个很好用的Hibernate_Annotation...Hibernate/EBJ 3 注释也可以为主键的自 动生成提供丰富的支持,允许实现各种策略...
Hibernate注解详解(超全面不解释)
Hibernate注解详解(超全面不解释)_计算机软件及应用_IT/计算机_专业资料。一。...(3) 列 boolean updatable() default true; (5) 个列 String column...
Hibernate最详细实例教程_图文
Hibernate最详细实例教程 Hibernate hibernate 配置文件注解 <!--表明解析本 XML ..."); } %> 建表文件 CREATE TABLE `member` ( `id` int(3) unsigned ...
Hibernate注解方法使用总结
Hibernate 注解方法使用总结 (2013-07-29 18:29:09) 转载▼ 分类: Hibernate...public B getB(){ return b; } 从表:无 3.主表 A 中有一个从表属性...
hibernate注释详解
hibernate注释详解_计算机软件及应用_IT/计算机_专业资料。为了学习 hibernate 的...3.hilo 要在数据库中建立一张额外的表,默认表名为 hibernate_unque_key,默认...
Struts2.1.6+Spring2.5.6+Hibernate3.3.1全注解实例详解
Struts2.1.6+Spring2.5.6+Hibernate3.3.1全注解实例详解_计算机软件及应用_IT/计算机_专业资料。一套完整的SSH框架搭建过程,并附上图文注释1...
hibernate注解大全
hibernate注解大全_计算机软件及应用_IT/计算机_专业资料。hibernate注解大全 ...(2) 是否在该列上设置唯一约束 boolean nullable() default true; (3) 列可...
hibernate注解详解
hibernate注解详解_计算机软件及应用_IT/计算机_专业资料。hibernate4 学习@...public class Custom { 2. 3. @OneToOne @JoinColumn(name="CUST_ID", ...
更多相关标签: