您好,登錄后才能下訂單哦!
學生 和 課程就是一種多對多的關系映射,那么再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(); } @Override public 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(); } @Override public 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. 測試
查詢和更新測試略
查詢直接查就可以了,更新查詢后,修改值后,更新就可以了。
插入測試,級聯插入
@Test public 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);
那么插入中,再關聯表中,由于乙方已經維護,此時已經存在關聯關系,這時候,另一方已進行維護
那么由于已經存在關聯關系,這時候會報 違反唯一約束條件
刪除測試
@Test public 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 會是空指針 course.getStudents().add(students);
2. 級聯關系
在關聯關系中,要慎用關系,以免對另一方數據操作損失。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。