Hibernate5.2之多对多关联关系(六)

一.简介

  Hibernate中多对多关联关系在工程中使用的频率也是非常高的,例如:大学中学生选课、用户订单等。在本博客中笔者一样通过实例代码为读者阐述Hibernate中的这种关联关系,笔者所采用的是学生选课的案例。

二.数据库的创建

create table courses (
id number(10,0) not null,
cname varchar2(255 char),
primary key (id)
); create table stu_course (
student_id number(10,0) not null,
course_id number(10,0) not null,
primary key (course_id, student_id)
); create table students (
id number(10,0) not null,
name varchar2(255 char),
primary key (id)
);

三.hbm配置文件的方式

3.1 POJO类的创建

public class Student {
private int id;
private String name;
private Set<Course> courses; //setter and getter
} public class Course {
private int id;
private String name;
private Set<Student> students; //setter and getter
}

3.2 hbm文件的创建

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.demo.hibernate.many2many.Student" table="students">
<id name="id" type="int" column="id">
<generator class="increment"></generator>
</id> <property name="name" type="string" column="name"></property> <set name="courses" table="stu_course">
<key column="student_id"></key>
<many-to-many class="com.demo.hibernate.many2many.Course" column="course_id"></many-to-many>
</set>
</class>
</hibernate-mapping>

Course.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.demo.hibernate.many2many.Course" table="courses">
<id name="id" type="int" column="id">
<generator class="increment"></generator>
</id> <property name="name" type="string" column="cname"></property> <set name="students" table="stu_course" inverse="true">
<key column="course_id"></key>
<many-to-many class="com.demo.hibernate.many2many.Student" column="student_id"></many-to-many>
</set>
</class>
</hibernate-mapping>

四.注解的方式

Student.java

@Entity
@Table(name="students")
public class Student { @Id
@Column(name="id")
@GenericGenerator(name="incrementGenerator", strategy="increment")
@GeneratedValue(generator="incrementGenerator")
private int id; @Column(name="name")
private String name; @ManyToMany
@JoinTable(name="stu_course",
joinColumns={@JoinColumn(name="student_id")},
inverseJoinColumns={@JoinColumn(name="course_id")})
private Set<Course> courses; //setter and getter
}

Course.java

@Entity
@Table(name="courses")
public class Course { @Id
@Column(name="id")
@GenericGenerator(name="incrementGenerator", strategy="increment")
@GeneratedValue(generator="incrementGenerator")
private int id; @Column(name="cname")
private String name; @ManyToMany(mappedBy="courses")
private Set<Student> students; //setter and getter
}

五.代码测试

A.保存

@Test
public void save(){
Transaction tx = session.beginTransaction(); Student s1 = new Student();
s1.setName("BBB"); Course c1 = new Course();
c1.setName("音乐"); Set<Student> students = new HashSet<Student>();
Set<Course> courses = new HashSet<Course>(); students.add(s1);
courses.add(c1); s1.setCourses(courses);
c1.setStudents(students); session.save(s1);
session.save(c1); tx.commit();
}

B.更新1

//新开了一门课,然后学生选择了该课程
@Test
public void addCourse(){
Transaction tx = session.beginTransaction();
Student student = session.get(Student.class, 1);
Course c1 = new Course();
c1.setName("音乐"); student.getCourses().add(c1);
session.saveOrUpdate(c1);
session.saveOrUpdate(student);
tx.commit();
}

C.更新2

//新增学生,然后学生选择课程
@Test
public void addStudent(){
Transaction tx = session.beginTransaction();
Student s = new Student();
s.setName("HHH"); Query<Course> query = session.createQuery("from Course where id in :ids", Course.class);
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
query.setParameter("ids", list); Set<Course> courses = new HashSet<Course>(query.getResultList()); s.setCourses(courses);
session.save(s);
tx.commit();
}

D.更新3

//学生改变课程
@Test
public void changeCourse(){
Transaction tx = session.beginTransaction();
Student student = session.get(Student.class, 1); Course course = session.get(Course.class, 2);
Set<Course> courses = new HashSet<Course>();
courses.add(course); student.setCourses(courses); session.update(student);
tx.commit();
}

E.删除

//删除了学生
@Test
public void delete(){
Transaction tx = session.beginTransaction();
Student s = new Student();
s.setId(1); session.delete(s);
tx.commit();
}
05-11 11:13