一對(duì)多也是分為兩種:
- 單向一對(duì)多
- 雙向一對(duì)多
先來看一下公用的實(shí)體類
classes實(shí)體類
package entity;
import java.util.Set;
public class Classes {
private int id;
private String className;
private Set<Student> student;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public Set<Student> getStudent() {
return student;
}
public void setStudent(Set<Student> student) {
this.student = student;
}
}
student實(shí)體類(單向的時(shí)候沒有class這個(gè)屬性)
package entity;
public class Student {
private int id;
private String name;
private Classes classes;
public Classes getClasses() {
return classes;
}
public void setClasses(Classes classes) {
this.classes = classes;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
單向一對(duì)多
classes.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="entity">
<class name="Classes" dynamic-update="true" >
<id name="id">
<generator class="native"/>
</id>
<property name="className" />
<set name="student" cascade="delete">
<!-- 這里的column是指數(shù)據(jù)庫中student表中會(huì)添加外鍵classid,關(guān)聯(lián)classes表的主鍵 -->
<key column="classid"></key>
<one-to-many class="Student" />
</set>
</class>
</hibernate-mapping>
student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="entity">
<class name="Student" dynamic-update="true">
<id name="id">
<generator class="native"/>
</id>
<property name="name" />
</class>
</hibernate-mapping>
單元測試:
package entity;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.hibernate.classic.Session;
import junit.framework.TestCase;
import util.hibernateUtil;
/**
* 單向一對(duì)多(項(xiàng)目名稱寫錯(cuò)了邑时,應(yīng)該是一對(duì)多)
* 測試目的:
* 1. 新增數(shù)據(jù)拜隧,是否會(huì)級(jí)聯(lián)保存下面?什么時(shí)候發(fā)出sql語句蚪战?
* 2. 查詢數(shù)據(jù),會(huì)不會(huì)級(jí)聯(lián)查詢庸推?
* 3. 刪除數(shù)據(jù)蒜茴,會(huì)不會(huì)級(jí)聯(lián)刪除?
* @author arkulo
*
*/
public class testManytoone extends TestCase {
// 新增數(shù)據(jù)
public void test1() {
Session session = null;
try {
session = hibernateUtil.getSession();
session.beginTransaction();
Student st1 = new Student();
st1.setName("張三");
Student st2 = new Student();
st2.setName("李四");
Student st3 = new Student();
st3.setName("王五");
// 這里必須先保存三個(gè)student疆栏,否則它們沒有ID曾掂,最后會(huì)拋異常
// 這是sql執(zhí)行的順序
// Hibernate: insert into Student (name) values (?)
// Hibernate: insert into Student (name) values (?)
// Hibernate: insert into Student (name) values (?)
// Hibernate: insert into Classes (className) values (?)
// Hibernate: update Student set classid=? where id=?
// Hibernate: update Student set classid=? where id=?
// Hibernate: update Student set classid=? where id=?
session.save(st1);
session.save(st2);
session.save(st3);
// 這屬于在one的一方維護(hù)關(guān)系
Set<Student> set = new HashSet<Student>();
set.add(st1);
set.add(st2);
set.add(st3);
Classes cl = new Classes();
cl.setClassName("一年級(jí)三班");
cl.setStudent(set);
session.save(cl);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
} finally {
hibernateUtil.closeSession(session);
}
}
// 查詢數(shù)據(jù)
public void test2() {
test1();
System.out.println("-------------------------------------------------");
Session session = null;
try {
session = hibernateUtil.getSession();
session.beginTransaction();
// 發(fā)出兩條sql語句
// 如果沒有緩存的情況下,這里的iterate會(huì)產(chǎn)生n+1問題
Iterator st = session.createQuery("from Classes").iterate();
while (st.hasNext()) {
Classes cl = (Classes) st.next();
System.out.println("班級(jí):" + cl.getClassName());
Set<Student> students = cl.getStudent();
Iterator<Student> it = students.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println("用戶名:" + s.getName());
}
}
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
} finally {
hibernateUtil.closeSession(session);
}
}
// 刪除數(shù)據(jù)
public void test3() {
test1();
System.out.println("-------------------------------------------------");
Session session = null;
try {
session = hibernateUtil.getSession();
session.beginTransaction();
// 再不設(shè)置級(jí)聯(lián)的情況下壁顶,刪除classes珠洗,是否會(huì)刪除student的記錄
Classes cl = (Classes) session.get(Classes.class, 1);
System.out.println("班級(jí)名稱:" + cl.getClassName());
Set<Student> students = cl.getStudent();
Iterator<Student> it = students.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println("學(xué)生姓名:" + s.getName());
}
System.out.println("------------------------------------------");
// 不設(shè)置級(jí)聯(lián)關(guān)系為delete的情況下,刪除classes若专,會(huì)自動(dòng)的把student表中的classid字段設(shè)置為null
// 設(shè)置級(jí)聯(lián)關(guān)系為delete后许蓖,sql語句順序?yàn)橄拢? // Hibernate: update Student set classid=null where classid=?
// Hibernate: delete from Student where id=?
// Hibernate: delete from Student where id=?
// Hibernate: delete from Student where id=?
// Hibernate: delete from Classes where id=?
// 可以看出,即使設(shè)置了級(jí)聯(lián)為delete调衰,也會(huì)先更新classid字段為null膊爪,然后在刪除student,最后刪除classes
session.delete(cl);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
} finally {
hibernateUtil.closeSession(session);
}
}
}
雙向一對(duì)多
classes.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="entity">
<class name="Classes" dynamic-update="true" >
<id name="id">
<generator class="native"/>
</id>
<property name="className" />
<set name="student" cascade="delete" inverse="true">
<!-- 這里的column是指數(shù)據(jù)庫中student表中會(huì)添加外鍵classid嚎莉,關(guān)聯(lián)classes表的主鍵 -->
<key column="classid"></key>
<one-to-many class="Student" />
</set>
</class>
</hibernate-mapping>
student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="entity">
<class name="Student" dynamic-update="true">
<id name="id">
<generator class="native"/>
</id>
<property name="name" />
<many-to-one name="classes" column="classid"></many-to-one>
</class>
</hibernate-mapping>
單元測試:
package entity;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.hibernate.classic.Session;
import junit.framework.TestCase;
import util.hibernateUtil;
/**
* 雙向一對(duì)多(一般情況下米酬,不推薦在one的一方維護(hù)關(guān)系)
*
* 測試目的
* 0. 使many方的設(shè)置失效,由one方維護(hù)關(guān)系
* 1. 新增數(shù)據(jù)趋箩,是否會(huì)級(jí)聯(lián)保存赃额?什么時(shí)候發(fā)出sql語句加派?
* 2. 查詢數(shù)據(jù),會(huì)不會(huì)級(jí)聯(lián)查詢跳芳?
* 3. 刪除數(shù)據(jù)芍锦,會(huì)不會(huì)級(jí)聯(lián)刪除?
* @author arkulo
*
*/
public class testManytoone extends TestCase {
// 新增數(shù)據(jù)
public void test1() {
Session session = null;
try {
session = hibernateUtil.getSession();
session.beginTransaction();
Classes cl = new Classes();
cl.setClassName("一年級(jí)三班");
session.save(cl);
// 這是在many端維護(hù)的關(guān)系飞盆,比較簡單娄琉,起碼不用中間還需要一個(gè)set集合
Student st1 = new Student();
st1.setName("張三");
st1.setClasses(cl);
session.save(st1);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
} finally {
hibernateUtil.closeSession(session);
}
}
// 查詢數(shù)據(jù)
public void test2() {
test1();
System.out.println("-------------------------------------------------");
Session session = null;
try {
session = hibernateUtil.getSession();
session.beginTransaction();
// 發(fā)出兩條sql語句
// 如果沒有緩存的情況下,這里的iterate會(huì)產(chǎn)生n+1問題
Iterator st = session.createQuery("from Classes").iterate();
while (st.hasNext()) {
Classes cl = (Classes) st.next();
System.out.println("班級(jí):" + cl.getClassName());
Set<Student> students = cl.getStudent();
Iterator<Student> it = students.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println("用戶名:" + s.getName());
}
}
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
} finally {
hibernateUtil.closeSession(session);
}
}
// 刪除數(shù)據(jù)
public void test3() {
test1();
System.out.println("-------------------------------------------------");
Session session = null;
try {
session = hibernateUtil.getSession();
session.beginTransaction();
// 再不設(shè)置級(jí)聯(lián)的情況下吓歇,刪除classes车胡,是否會(huì)刪除student的記錄
Classes cl = (Classes) session.get(Classes.class, 1);
System.out.println("班級(jí)名稱:" + cl.getClassName());
Set<Student> students = cl.getStudent();
Iterator<Student> it = students.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println("學(xué)生姓名:" + s.getName());
}
System.out.println("------------------------------------------");
// 不設(shè)置級(jí)聯(lián)關(guān)系為delete的情況下,刪除classes照瘾,系統(tǒng)會(huì)提示外鍵關(guān)聯(lián)并拋出異常
// 在student映射文件上設(shè)置級(jí)聯(lián)為delete匈棘,系統(tǒng)也會(huì)報(bào)錯(cuò),因?yàn)檫@里我們刪除的是classes
// 在classes上設(shè)置級(jí)聯(lián)關(guān)系為delete后析命,sql語句順序?yàn)橄拢? // Hibernate: delete from Student where id=?
// Hibernate: delete from Classes where id=?
// 這樣就實(shí)現(xiàn)了級(jí)聯(lián)刪除
session.delete(cl);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
} finally {
hibernateUtil.closeSession(session);
}
}
}