jpa双方向多対多関連関係

4845 ワード

1.1.関係メンテナ
Teacher.java
package com.morris.entity;
 
import java.util.Set;
 
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
 
@Entity
public class Teacher {
   
    @Id
    @GeneratedValue
    private Integer id;
   
    @Column(name="name",length=50,nullable=false)
    private String name;
   
    @ManyToMany(cascade=CascadeType.REFRESH,fetch=FetchType.LAZY)
    @JoinTable(name="stu_tea",joinColumns=@JoinColumn(name="stu_id"),inverseJoinColumns=@JoinColumn(name="tea_id"))
    private Set<Student> students;
   
    @Override
    public String toString() {
       return "Teacher [id=" + id + ", name=" + name + "]";
    }
 
    public Teacher() {
       super();
       // TODO Auto-generated constructor stub
    }
 
    public Teacher(String name) {
       super();
       this.name = name;
    }
 
    public Set<Student> getStudents() {
       return students;
    }
 
    public void setStudents(Set<Student> students) {
       this.students = students;
    }
 
    public Integer getId() {
       return id;
    }
 
    public void setId(Integer id) {
       this.id = id;
    }
 
    public String getName() {
       return name;
    }
 
    public void setName(String name) {
       this.name = name;
    }
}
 

1.2.関係被保守者
Student.java
package com.morris.entity;
 
import java.util.Set;
 
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
 
@Entity
public class Student {
   
    @Id
    @GeneratedValue
    private Integer id;
   
    @Column(name="name",nullable=false,length=50)
    private String name;
   
    @ManyToMany(cascade=CascadeType.REFRESH,fetch=FetchType.LAZY,mappedBy="students")
    private Set<Teacher> teachers;
 
    public Integer getId() {
       return id;
    }
 
    public void setId(Integer id) {
       this.id = id;
    }
 
    public String getName() {
       return name;
    }
 
    public void setName(String name) {
       this.name = name;
    }
 
    public Student(String name) {
       super();
       this.name = name;
    }
 
    public Set<Teacher> getTeachers() {
       return teachers;
    }
 
    public void setTeachers(Set<Teacher> teachers) {
       this.teachers = teachers;
    }
 
    public Student() {
       super();
       // TODO Auto-generated constructor stub
    }
 
    @Override
    public String toString() {
       return "Student [id=" + id + ", name=" + name + "]";
    }
   
   
 
}
 
1.3.テスト
Many2ManyTest.java
package com.morris.test;
 
import static org.junit.Assert.*;
 
import java.util.HashSet;
import java.util.Set;
 
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
 
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
 
import com.morris.entity.Student;
import com.morris.entity.Teacher;
 
public class Many2ManyTest {
 
       private static EntityManagerFactory entityManagerFactory;
      
       private static EntityManager entityManager;
      
       @BeforeClass
       public static void setUpBeforeClass() throws Exception {
             
              entityManagerFactory = Persistence.createEntityManagerFactory("jpaDemo");
             
              entityManager = entityManagerFactory.createEntityManager();
             
              entityManager.getTransaction().begin();
      
       }
 
       @Test
       public void test() {
             
              Teacher teacher = new Teacher("  ");
              Student student = new Student("  ");
             
              entityManager.persist(teacher);
              entityManager.persist(student);
             
              Set<Student> students = new HashSet<Student>();
              students.add(student);
              teacher.setStudents(students);
             
             
             
       }
      
 
       @AfterClass
       public static void tearDownAfterClass() throws Exception {
             
              entityManager.getTransaction().commit();
             
              entityManager.close();
              entityManagerFactory.close();
       }
      
 
}