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

运行以上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 (?, ?, ?)

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值