现在的位置: 首页 > 综合 > 正文

hibernate中注解Annotation总结大全

2013年10月24日 ⁄ 综合 ⁄ 共 34062字 ⁄ 字号 评论关闭

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")},inverseJoinColumns={@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 ,mappedBy="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")},inverseJoinColumns={@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.     @Id @GeneratedValue  
  20.     public Integer getAutoId() {  
  21.         return autoId;  
  22.     }  
  23.     public void setAutoId(Integer autoId) {  
  24.         this.autoId = autoId;  
  25.     }  
  26.     public String getAutotype() {  
  27.         return autotype;  
  28.     }  
  29.     public void setAutotype(String autotype) {  
  30.         this.autotype = autotype;  
  31.     }  
  32.     public String getAutonum() {  
  33.         return autonum;  
  34.     }  
  35.     public void setAutonum(String autonum) {  
  36.         this.autonum = autonum;  
  37.     }  
  38.     @ManyToOne  
  39.     @JoinColumn(name="garageid")  
  40.     public Garage getGarage() {  
  41.         return garage;  
  42.     }  
  43.     public void setGarage(Garage garage) {  
  44.         this.garage = garage;  
  45.     }  
  46.   
  47. }  
  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 factory = Persistence.createEntityManagerFactory("jpa-hibernate");  
  99.         EntityManager em = factory.createEntityManager();  
 100.         em.getTransaction().begin();  
 101.           
 102.         Garage garage = new Garage();  
 103.         garage.setGaragenum("room1");  
 104.           
 105.         Auto auto1 = new Auto();  
 106.         auto1.setAutonum("bj0000");  
 107.         auto1.setAutotype("car");  
 108.           
 109.         Auto auto2 = new Auto();  
 110.         auto2.setAutonum("bj1231");  
 111.         auto2.setAutotype("bus");  
 112.           
 113.         garage.addGarageAuto(auto1);  
 114.         garage.addGarageAuto(auto2);  
 115.           
 116.         em.persist(garage);  
 117.         em.getTransaction().commit();  
 118.         em.close();  
 119.         factory.close();  
 120.     }  

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.      * 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.     @Column(length=20)  
  31.     public String getGaragenum() {  
  32.         return garagenum;  
  33.     }  
  34.     public void setGaragenum(String garagenum) {  
  35.         this.garagenum = garagenum;  
  36.     }  
  37.     @OneToMany(cascade={CascadeType.PERSIST},mappedBy="garage")  
  38.     public Set<Auto> getAutos() {  
  39.         return autos;  
  40.     }  
  41.     public void setAutos(Set<Auto> autos) {  
  42.         this.autos = autos;  
  43.     }  
  44.     public void addGarageAuto(Auto auto) {  
  45.         auto.setGarage(this);  
  46.         this.autos.add(auto);  
  47.     }  
  48.   
  49. }  

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

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 factory = 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. }  

@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.     @Column(length=20)  
  31.     public String getGaragenum() {  
  32.         return garagenum;  
  33.     }  
  34.     public void setGaragenum(String garagenum) {  
  35.         this.garagenum = garagenum;  
  36.     }  
  37.     @OneToMany(cascade={CascadeType.PERSIST},mappedBy="garage")  
  38.     public Set<Auto> getAutos() {  
  39.         return autos;  
  40.     }  
  41.     public void setAutos(Set<Auto> autos) {  
  42.         this.autos = autos;  
  43.     }  
  44.     public void addGarageAuto(Auto auto) {  
  45.         auto.setGarage(this);  
  46.         this.autos.add(auto);  
  47.     }  
  48.   
  49. }  

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

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 factory = 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. }  

@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.      * many to one jpa 多对一 hibernate实现 
  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.     @Column(length=20)  
  31.     public String getGaragenum() {  
  32.         return garagenum;  
  33.     }  
  34.     public void setGaragenum(String garagenum) {  
  35.         this.garagenum = garagenum;  
  36.     }  
  37.     @OneToMany(cascade={CascadeType.PERSIST},mappedBy="garage")  
  38.     public Set<Auto> getAutos() {  
  39.         return autos;  
  40.

抱歉!评论已关闭.