JPA关联映射 - 多对多映射

来源:互联网 发布:兼职淘宝服装试衣模特 编辑:程序博客网 时间:2024/05/18 00:50

1.persistence.xml

JPA规范要求在类路径的META-INF目录下放置persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"version="1.0"><persistence-unit name="itcast" transaction-type="RESOURCE_LOCAL"><properties><property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver" /><property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8" /><property name="hibernate.connection.username" value="root" /><property name="hibernate.connection.password" value="root" /><property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" /><property name="hibernate.hbm2ddl.auto" value="update" /><property name="hibernate.show_sql" value="true" /><property name="hibernate.format_sql" value="true" /><property name="hibernate.max_fetch_depth" value="1" /></properties></persistence-unit></persistence>
2.实体类
(1)多的一方(关系维护方)

package cn.itcast.domain;import java.util.HashSet;import java.util.Set;import javax.persistence.CascadeType;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.JoinTable;import javax.persistence.ManyToMany;/** * Student学员实体类 */@Entitypublic class Student {/** 主键 **/private Integer id;/** 学员名称 **/private String name;/** 学员 - 教师 **/private Set<Teacher> teachers = new HashSet<Teacher>();public Student() {}public Student(String name) {this.name = name;}@Id@GeneratedValuepublic Integer getId() {return id;}public void setId(Integer id) {this.id = id;}@Column(length = 12, nullable = false, unique = true)public String getName() {return name;}public void setName(String name) {this.name = name;}@ManyToMany(cascade = CascadeType.REFRESH)@JoinTable(name = "student_teacher", joinColumns = @JoinColumn(name = "student_id"),inverseJoinColumns = @JoinColumn(name = "teacher_id"))public Set<Teacher> getTeachers() {return teachers;}public void setTeachers(Set<Teacher> teachers) {this.teachers = teachers;}/** * 添加学员的授课教师 */public void addTeacher(Teacher teacher) {this.teachers.add(teacher);}/** * 移除学员的授课教师 */public void removeTeacher(Teacher teacher) {if (this.teachers.contains(teacher)) {this.teachers.remove(teacher);}}}
(2)多的一方(关系呗维护方)
package cn.itcast.domain;import java.util.HashSet;import java.util.Set;import javax.persistence.CascadeType;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.ManyToMany;/** * Teacher教师实体类 */@Entitypublic class Teacher {/** 主键 **/private Integer id;/** 教师名称 **/private String name;/** 教师 - 学员 **/private Set<Student> students = new HashSet<Student>();public Teacher(){}public Teacher(String name) {this.name = name;}@Id @GeneratedValuepublic Integer getId() {return id;}public void setId(Integer id) {this.id = id;}@Column(length=12,unique=true)public String getName() {return name;}public void setName(String name) {this.name = name;}@ManyToMany(cascade=CascadeType.REFRESH,mappedBy="teachers")public Set<Student> getStudents() {return students;}public void setStudents(Set<Student> students) {this.students = students;}}
3.多对多单元测试类StudentTest
package junit.test;import javax.persistence.EntityManager;import javax.persistence.Persistence;import org.junit.AfterClass;import org.junit.BeforeClass;import org.junit.Test;import cn.itcast.domain.Student;import cn.itcast.domain.Teacher;/** * 多对多的单元测试类 */public class StudentTest {private static EntityManager em;@BeforeClasspublic static void setUpBeforeClass() throws Exception {em = Persistence.createEntityManagerFactory("itcast").createEntityManager();}@AfterClasspublic static void tearDownAfterClass() throws Exception {em.close();}@Testpublic void testSave () {em.getTransaction().begin();em.persist(new Student("小张1"));em.persist(new Teacher("李勇老师1"));em.getTransaction().commit();}/** * 建立老师和学生的关系 */@Testpublic void testBuildTS () {em.getTransaction().begin();Student student = em.find(Student.class, 1);Teacher teacher = em.getReference(Teacher.class, 1);student.addTeacher(teacher);em.getTransaction().commit();}/** * 解除老师和学生的关系 */@Testpublic void testDeleteTS () {em.getTransaction().begin();Student student = em.find(Student.class, 1);Teacher teacher = em.getReference(Teacher.class, 1);student.removeTeacher(teacher);em.getTransaction().commit();}/** * 删除老师(当删除教师时,需要先接触与其相关的学员的关系,然后再对其进行删除) */@Testpublic void testDeleteTeacher () {em.getTransaction().begin();Student student = em.find(Student.class, 1);Teacher teacher = em.getReference(Teacher.class, 1);student.removeTeacher(teacher);em.remove(teacher);em.getTransaction().commit();}}


0 0
原创粉丝点击