日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

六、hibernate表与表之间的关系(多对多关系)

發布時間:2024/3/7 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 六、hibernate表与表之间的关系(多对多关系) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

多對多關系

創建實體類和對應映射文件

Student.java

1 package com.qf.entity; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 public class Student { 7 8 private Long sid; 9 private String sname; 10 //一個學生可以有很多個老師 11 private Set<Teacher> teachs = new HashSet<>(); 12 13 public Long getSid() { 14 return sid; 15 } 16 public void setSid(Long sid) { 17 this.sid = sid; 18 } 19 20 public String getSname() { 21 return sname; 22 } 23 public void setSname(String sname) { 24 this.sname = sname; 25 } 26 27 public Set<Teacher> getTeachs() { 28 return teachs; 29 } 30 public void setTeachs(Set<Teacher> teachs) { 31 this.teachs = teachs; 32 } 33 @Override 34 public String toString() { 35 return "Student [sid=" + sid + ", sname=" + sname + ", teachs=" + teachs + "]"; 36 } 37 38 } View Code

Student.hbm.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping><!-- 配置表與實體的映射關系 --><class name="com.qf.entity.Student" table="student"><id name="sid" column="sid"><generator class="native"></generator></id><property name="sname" column="sname"/><set name="teachs" table="teach_stu" ><key column="sid"/><many-to-many class="com.qf.entity.Teacher" column="tid" /></set></class> </hibernate-mapping>

Teacher.java

1 package com.qf.entity; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 public class Teacher { 7 private long tid; 8 private String tname; 9 private String course; 10 //一個教師可以有多個學生 11 private Set<Student> stus = new HashSet<Student>(); 12 13 public Set<Student> getStus() { 14 return stus; 15 } 16 public void setStus(Set<Student> stus) { 17 this.stus = stus; 18 } 19 public long getTid() { 20 return tid; 21 } 22 public void setTid(long tid) { 23 this.tid = tid; 24 } 25 public String getTname() { 26 return tname; 27 } 28 public void setTname(String tname) { 29 this.tname = tname; 30 } 31 public String getCourse() { 32 return course; 33 } 34 public void setCourse(String course) { 35 this.course = course; 36 } 37 @Override 38 public String toString() { 39 return "Teacher [tid=" + tid + ", tname=" + tname + ", course=" + course + "]"; 40 } 41 42 43 } View Code

Teacher.hbm.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping><!-- 配置表與實體的映射關系 --><class name="com.qf.entity.Teacher" table="teacher"><id name="tid" column="tid"><generator class="native"/></id><property name="tname" column="tname"/><property name="course" column="course"/><set name="stus" table="teach_stu"><key column="tid"></key><many-to-many class="com.qf.entity.Student" column="sid"/></set></class> </hibernate-mapping> 

測試

@Test public void test() {Session session = SessionFactoryUtil.getSession();Transaction tx = session.beginTransaction();//創建兩個教師對象,三個學生對象//學生選擇老師Teacher t1 = new Teacher();t1.setTname("張老師");Teacher t2 = new Teacher();t2.setTname("李老師");Student s1 = new Student();s1.setSname("張三");Student s2 = new Student();s2.setSname("李四");Student s3 = new Student();s3.setSname("王五");//學生和老師之間建立雙向關系//多對多關系建立雙向關系后,雙方必須有一方放棄維護外鍵關系權(inverse),否則會因為中間表主鍵重復拋出異常s1.getTeachs().add(t1);s1.getTeachs().add(t2);s3.getTeachs().add(t1);s2.getTeachs().add(t2);t1.getStus().add(s1);t1.getStus().add(s3);t2.getStus().add(s2);t2.getStus().add(s1);session.save(s1);session.save(s2);session.save(s3);session.save(t1);session.save(t2);tx.commit(); }

-------------------------------------console-------------------------------------

Hibernate: alter table teach_stu drop foreign key FKsrw3nh8oe5xhmqqxm2qng952t Hibernate: alter table teach_stu drop foreign key FKtc48sy6cweqkufd4c777i8vsp Hibernate: drop table if exists student Hibernate: drop table if exists teach_stu Hibernate: drop table if exists teacher Hibernate: create table student (sid bigint not null auto_increment, sname varchar(255), primary key (sid)) Hibernate: create table teach_stu (tid bigint not null, sid bigint not null, primary key (sid, tid)) Hibernate: create table teacher (tid bigint not null auto_increment, tname varchar(255), course varchar(255), primary key (tid)) Hibernate: alter table teach_stu add constraint FKsrw3nh8oe5xhmqqxm2qng952t foreign key (sid) references student (sid) Hibernate: alter table teach_stu add constraint FKtc48sy6cweqkufd4c777i8vsp foreign key (tid) references teacher (tid) 11:21:37,135 INFO SchemaExport:464 - HHH000230: Schema export complete Hibernate: insert into student (sname) values (?) Hibernate: insert into student (sname) values (?) Hibernate: insert into student (sname) values (?) Hibernate: insert into teacher (tname, course) values (?, ?) Hibernate: insert into teacher (tname, course) values (?, ?) Hibernate: insert into teach_stu (sid, tid) values (?, ?) Hibernate: insert into teach_stu (sid, tid) values (?, ?) Hibernate: insert into teach_stu (sid, tid) values (?, ?) Hibernate: insert into teach_stu (sid, tid) values (?, ?) Hibernate: insert into teach_stu (tid, sid) values (?, ?) 11:21:37,270 WARN SqlExceptionHelper:127 - SQL Error: 1062, SQLState: 23000 11:21:37,271 ERROR SqlExceptionHelper:129 - Duplicate entry '1-1' for key 'PRIMARY' 11:21:37,271 INFO AbstractBatchImpl:193 - HHH000010: On release of batch it still contained JDBC statements 11:21:37,272 ERROR SessionImpl:2994 - HHH000346: Error during managed flush [could not execute statement]

原因

  測試中因為建立了雙向關系,所以teacher和student都會去維護外鍵,導致相同的(1,1)會兩次插入中間關系表teach_stu中

解決辦法

  一方放棄維護外鍵關系權。一般被動主體放棄維護外鍵權,比如學生選擇老師,老師放棄維護外鍵權;老師選擇學生,學生放棄維護外鍵權

修改Teacher.hbm.xml:<set name="stus" table="teach_stu" inverse="true">

-------------------------------------console-------------------------------------

Hibernate: alter table teach_stu drop foreign key FKsrw3nh8oe5xhmqqxm2qng952t Hibernate: alter table teach_stu drop foreign key FKtc48sy6cweqkufd4c777i8vsp Hibernate: drop table if exists student Hibernate: drop table if exists teach_stu Hibernate: drop table if exists teacher Hibernate: create table student (sid bigint not null auto_increment, sname varchar(255), primary key (sid)) Hibernate: create table teach_stu (tid bigint not null, sid bigint not null, primary key (sid, tid)) Hibernate: create table teacher (tid bigint not null auto_increment, tname varchar(255), course varchar(255), primary key (tid)) Hibernate: alter table teach_stu add constraint FKsrw3nh8oe5xhmqqxm2qng952t foreign key (sid) references student (sid) Hibernate: alter table teach_stu add constraint FKtc48sy6cweqkufd4c777i8vsp foreign key (tid) references teacher (tid) 11:30:43,077 INFO SchemaExport:464 - HHH000230: Schema export complete Hibernate: insert into student (sname) values (?) Hibernate: insert into student (sname) values (?) Hibernate: insert into student (sname) values (?) Hibernate: insert into teacher (tname, course) values (?, ?) Hibernate: insert into teacher (tname, course) values (?, ?) Hibernate: insert into teach_stu (sid, tid) values (?, ?) Hibernate: insert into teach_stu (sid, tid) values (?, ?) Hibernate: insert into teach_stu (sid, tid) values (?, ?) Hibernate: insert into teach_stu (sid, tid) values (?, ?)

數據成功插入數據庫

級聯保存

保存學生級聯保存教師

主體是學生,所以修改學生的映射文件Student.hbm.xml

<hibernate-mapping><!-- 配置表與實體的映射關系 --><class name="com.qf.entity.Student" table="student"><id name="sid" column="sid"><generator class="native"></generator></id><property name="sname" column="sname"/><set name="teachs" table="teach_stu" cascade="save-update" ><key column="sid"/><many-to-many class="com.qf.entity.Teacher" column="tid" /></set></class> </hibernate-mapping>

測試方法

@Test public void test() {Session session = SessionFactoryUtil.getSession();Transaction tx = session.beginTransaction();//保存學生級聯保存教師Teacher t1 = new Teacher();t1.setTname("張老師");Student s1 = new Student();s1.setSname("張三");t1.getStus().add(s1);s1.getTeachs().add(t1);session.save(s1);tx.commit(); }

-------------------------------------console-------------------------------------

Hibernate: create table student (sid bigint not null auto_increment, sname varchar(255), primary key (sid)) Hibernate: create table teach_stu (tid bigint not null, sid bigint not null, primary key (sid, tid)) Hibernate: create table teacher (tid bigint not null auto_increment, tname varchar(255), course varchar(255), primary key (tid)) Hibernate: alter table teach_stu add constraint FKsrw3nh8oe5xhmqqxm2qng952t foreign key (sid) references student (sid) Hibernate: alter table teach_stu add constraint FKtc48sy6cweqkufd4c777i8vsp foreign key (tid) references teacher (tid) 14:22:37,418 INFO SchemaExport:464 - HHH000230: Schema export complete Hibernate: insert into student (sname) values (?) Hibernate: insert into teacher (tname, course) values (?, ?) Hibernate: insert into teach_stu (sid, tid) values (?, ?

級聯刪除(一般不使用)

想以哪一方為主體刪除另一方,就在哪一方映射文件中配置cascade屬性:

  <set name="teachs" table="teach_stu" cascade="delete" >

?

轉載于:https://www.cnblogs.com/qf123/p/10173799.html

總結

以上是生活随笔為你收集整理的六、hibernate表与表之间的关系(多对多关系)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。