Hibernate 多对多映射

时间:2023-03-09 03:44:24
Hibernate   多对多映射
package com.entity.manytomany;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany; import com.entity.BaseEntity;
@Entity
public class Student extends BaseEntity{
private String name;
@ManyToMany
//学生是不稳定的一方 所以由学生来维护
@JoinTable(name="student_course",
joinColumns=@JoinColumn(name="s_id"),
inverseJoinColumns=@JoinColumn(name="c_id"))
//关联表的表名
//关联当前表的主键
//关联对方表的主键 ()内的name即关联表内的字段名 可以自己*设计
private List<Course> courses;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
} }

student.java

package com.entity.manytomany;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.ManyToMany; import com.entity.BaseEntity;
@Entity
public class Course extends BaseEntity{
private String name;
@ManyToMany(mappedBy ="courses")
//被学生类的courses对象维护
private List<Student> students;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
} }

Course.java

package com;

import java.util.ArrayList;
import java.util.List; import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.entity.manytomany.Course;
import com.entity.manytomany.Student; public class TestManyToMany {
private Session s;
private Transaction tran;
@Before
public void before(){
s=HibernateUtils.getSession();
tran=s.beginTransaction(); } public void manytomany(){
Course c1=new Course();
Course c2=new Course();
Course c3=new Course();
Student stu1=new Student();
Student stu2=new Student();
Student stu3=new Student();
stu1.setName("zhangsan");
stu2.setName("lisi");
stu3.setName("wangwu");
c1.setName("java");
c2.setName("oracle");
c3.setName("html");
//新建对象
List<Course> courses =new ArrayList<Course>();//建立了一个课程集合
List<Course> courses1 =new ArrayList<Course>();
courses1.add(c2);
courses1.add(c3);
courses.add(c1);
courses.add(c2);
courses.add(c3);
//向集合中插入要选的课程
stu1.setCourses(courses);
stu2.setCourses(courses);
stu3.setCourses(courses1);
//建立关联 将课程集合set到学生的课程属性中 即学生选择了集合中所有的课程
s.save(c1);
s.save(c2);
s.save(c3);
s.save(stu1);
s.save(stu2);
s.save(stu3);
}
@Test
public void manytomanyquery(){
Course c=(Course)s.get(Course.class, 1);
for(Student s:c.getStudents()){
System.out.println(c.getName()+"-------选这门课的学生"+s.getName());
}
Course c1=(Course)s.get(Course.class, 2);
for(Student s:c1.getStudents()){
System.out.println(c1.getName()+"-------选这门课的学生"+s.getName());
}
Course c2=(Course)s.get(Course.class, 3);
for(Student s:c2.getStudents()){
System.out.println(c1.getName()+"-------选这门课的学生"+s.getName());
}
}
@After
public void after(){
tran.commit();
s.close();
}
}

testmanytomany.java