hibernate学习(5)——多对多关系映射

时间:2023-04-19 21:06:13

1、创建实体和映射

package com.alice.hibernate03.vo;

import java.util.HashSet;
import java.util.Set; public class Student {
private Integer id;
private String sname;
private Set<Course> courses = new HashSet<Course>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Set<Course> getCourses() {
return courses;
}
public void setCourses(Set<Course> courses) {
this.courses = courses;
}
}
package com.alice.hibernate03.vo;

import java.util.HashSet;
import java.util.Set; public class Course {
private Integer id;
private String cname;
private Set<Student> students = new HashSet<Student>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
}

2、映射

<?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 package="com.alice.hibernate03.vo">
<class name="Student" table="t_student">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="sname" column="sname" type="string"></property>
<!-- 多对多关系 -->
<!--
set 表达集合
name: 集合的属性名
table:多对多中间表的表名
key 表达外键
column:引用我的外键名
many-to-many 表达多对多
class : 集合引用方的类型
column:对方在中间表的外键名
-->
<set name="courses" table="t_student_course" inverse="false" cascade="save-update">
<!--
key 用来描述外键
column : 外键的值
-->
<key column="sid"></key>
<many-to-many class="Course" column="cid"></many-to-many>
</set>
</class>
</hibernate-mapping>
<?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 package="com.alice.hibernate03.vo">
<class name="Course" table="t_course">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="cname" column="cname" type="string"></property>
<!-- 多对多关系 -->
<!--
set 表达集合
name: 集合的属性名
table:多对多中间表的表名
key 表达外键
column:引用我的外键名
many-to-many 表达多对多
class : 集合引用方的类型
column:对方在中间表的外键名
-->
<set name="students" table="t_student_course" inverse="true" cascade="save-update">
<key column="cid"></key>
<many-to-many class="Student" column="sid"></many-to-many>
</set>
</class>
</hibernate-mapping>

  3、测试

package com.alice.hibernate03.many;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test; import com.alice.hibernate03.util.HibernateUtil;
import com.alice.hibernate03.vo.Course;
import com.alice.hibernate03.vo.Student; public class ManyTest {
@Test
public void test01(){
//保存学生 => 通过学生保存课程. 由学生维护外键
//Student 的
//inverse = false
//cascade = save-update
//Course
//inverse = true
Session session = HibernateUtil.openSession();
Transaction tran = session.beginTransaction();
//...........................
Student s1 = new Student();
s1.setSname("angle"); Student s2 = new Student();
s2.setSname("echo"); Course c1 = new Course();
c1.setCname("jdbc"); Course c2 = new Course();
c2.setCname("spring"); Course c3 = new Course();
c3.setCname("strust2"); s1.getCourses().add(c1);
s1.getCourses().add(c2); s2.getCourses().add(c2);
s2.getCourses().add(c3); session.save(s1);
session.save(s2);
//...........................
tran.commit();
session.close();
}
}