標籤:hibernate 多對多關聯關係
學生 和 課程就是一種多對多的關係映射,那麼再hibernate中,對於多對多關係在怎麼配置?
和一些注意的事項?簡單測試下。
-
建表
-
實體
-
設定檔和映射
-
測試
1。使用oracle ,建表sql
create table students(id number(7) primary key, name nvarchar2(20), age number(2))create table course(id number(7) primary key, name nvarchar2(20), time nvarchar2(20))create table st_cou(st_id references students(id), cou_id references course(id), primary key (st_id,cou_id))
2. students 實體
package com.hibernate.entity;import java.util.HashSet;import java.util.Set;public class Students {private Integer id;private String name;private Integer age;private Set<Course> course = new HashSet<Course>();public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Set<Course> getCourse() {return course;}public void setCourse(Set<Course> course) {this.course = course;}public Students(Integer id, String name, Integer age) {super();this.id = id;this.name = name;this.age = age;}public Students() {super();}@Overridepublic String toString() {return "Students [id=" + id + ", name=" + name + ", age=" + age + ", course=" + course + "]";}}
3. course實體
package com.hibernate.entity;import java.util.HashSet;import java.util.Set;public class Course {private Integer id;private String name;private String time;private Set<Students> students = new HashSet<Students>();public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getTime() {return time;}public void setTime(String time) {this.time = time;}public Set<Students> getStudents() {return students;}public void setStudents(Set<Students> students) {this.students = students;}public Course(Integer id, String name, String time) {super();this.id = id;this.name = name;this.time = time;}public Course() {super();}@Overridepublic String toString() {return "Course [id=" + id + ", name=" + name + ", time=" + time + "]";}}
4.hibernate.cfg.xml 配置
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <!-- 添加配置資訊:資料庫連接參數 --><property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property><property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property><property name="hibernate.connection.username">zt</property><property name="hibernate.connection.password">zt</property><!-- 添加配置資訊:hibernate 自身屬性dialect :方言,指定資料類型,hibernate可以針對不同的資料庫做出相應的最佳化 --><property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property><property name="hibernate.show_sql">true</property><property name="hibernate.format_sql">true</property><!-- 為getCurrentSession增加配置 --><property name="current_session_context_class">thread</property><mapping resource="course.cfg.xml"/> </session-factory></hibernate-configuration>
course.hbm.xmc 映射配置
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.hibernate.entity"><class name="Students" table="students"><id name="id"><generator class="increment"></generator></id><property name="age"/><property name="name"/><!-- name:是實體中關聯的另一方屬性 cascade:是級聯層級 table:是 學生和 課程中間關聯的那張表 下面course配置和students配置是一樣的。 --><set name="course" cascade="all" table="st_cou"><!-- columun:是關係表中的自己的維護欄位 --><key column="st_id"></key><!-- class:是關係對方的實體column:是關係對方的維護欄位 --><many-to-many class="Course" column="cou_id"/></set></class><class name="Course" table="course"><id name="id"><generator class="increment"></generator></id><property name="name"/><property name="time"/> <set name="students" cascade="all" table="st_cou"><key column="cou_id"></key><many-to-many class="Students" column="st_id"></many-to-many></set></class></hibernate-mapping>
5. 測試
查詢和更新測試略
查詢直接查就可以了,更新查詢後,修改值後,更新就可以了。
插入測試,級聯插入
@Testpublic void inset(){Session session = HibUtil.getSession();Transaction tx = session.beginTransaction();Course course = new Course(null, "地理", "面貌");Students students = new Students(null, "蛋蛋", 18);students.getCourse().add(course);// course.getStudents().add(students);這句不用寫session.save(students);tx.commit();}
多對多中,插入,只要乙方進行維護就可以,如果二方都寫
students.getCourse().add(course);course.getStudents().add(students);
那麼插入中,再關聯表中,由於乙方已經維護,此時已經存在關聯關係,這時候,另一方已進行維護
那麼由於已經存在關聯關係,這時候會報 違反唯一約束條件
刪除測試
@Testpublic void delete(){Session session = HibUtil.getSession();Transaction tx = session.beginTransaction();Students students = (Students)session.get(Students.class, 1);session.delete(students);tx.commit();}
執行之後,進入資料庫查詢,發現,只要是在關聯表中存在的,相關學生和課程都會被刪除。
配置中,我們配置了級聯層級是 all 這中許可權過大,慎用。級聯層級修改為 save-update
6.注意
1.實體中 我們寫關係一方有
private Set<Course> course = new HashSet<Course>();
為什麼 new HashSet<Course>();
當我們進行單個插入表時,如果沒有new HashSet<Course>();
Students students = (Students)session.get(Students.class, 1);Course course = new Course(null, "地理", "面貌");這裡的course.getStudents() 是個null 而用null對象.add 會是null 指標course.getStudents().add(students);
2. 級聯關係
在關聯關係中,要慎用關係,以免對另一方資料操作損失。
本文出自 “永恒之光” 部落格,請務必保留此出處http://zhuws.blog.51cto.com/11134439/1940750
hibernate 多對多關係映射