JPAマルチペアマルチインスタンス構成

6336 ワード

@Entity
public class Student {

	private Integer s_id;

	private String s_name;

	private Set<Teacher> teachers = new HashSet<Teacher>();

	public Student() {
		
	}

	public Student(String s_name) {
		this.s_name = s_name;
	}

	@Id
	@GeneratedValue
	public Integer getS_id() {
		return s_id;
	}

	public void setS_id(Integer s_id) {
		this.s_id = s_id;
	}

	@Column(nullable = false, length = 20)
	public String getS_name() {
		return s_name;
	}

	public void setS_name(String s_name) {
		this.s_name = s_name;
	}

	@ManyToMany(mappedBy = "students")
	public Set<Teacher> getTeachers() {
		return teachers;
	}

	public void setTeachers(Set<Teacher> teachers) {
		this.teachers = teachers;
	}

	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + ((s_id == null) ? 0 : s_id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Student other = (Student) obj;
		if (s_id == null) {
			if (other.s_id != null)
				return false;
		} else if (!s_id.equals(other.s_id))
			return false;
		return true;
	}

}

 
@Entity
public class Teacher {

	private Integer t_id;

	private String t_name;

	private Set<Student> students = new HashSet<Student>();

	public Teacher() {

	}

	public Teacher(String t_name) {
		this.t_name = t_name;
	}

	@Id
	@GeneratedValue
	public Integer getT_id() {
		return t_id;
	}

	public void setT_id(Integer t_id) {
		this.t_id = t_id;
	}

	@Column(nullable = false, length = 20)
	public String getT_name() {
		return t_name;
	}

	public void setT_name(String t_name) {
		this.t_name = t_name;
	}

	@ManyToMany()
	@JoinTable(name = "student_teacher", inverseJoinColumns = @JoinColumn(name = "student_id"), joinColumns = @JoinColumn(name = "teacher_id"))
	public Set<Student> getStudents() {
		return students;
	}

	public void setStudents(Set<Student> students) {
		this.students = students;
	}

	/**
	 *  
	 * 
	 * @param stu
	 */
	public void addStudent(Student stu) {
		this.students.add(stu);
	}

	/**
	 *  
	 * @param stu
	 */
	public void removeStudent(Student stu) {
		if(this.students.contains(stu)){
			this.students.remove(stu);
		}
	}
}

テストは次のとおりです.
public class JPA_ManyToManyTest {
	@Test
	public void manyTomany() throws Exception {
		EntityManagerFactory factory = Persistence
				.createEntityManagerFactory("mengya");
		factory.close();
	}

	/**
	 *  
	 * 
	 * @throws Exception
	 */
	@Test
	public void save() throws Exception {
		EntityManagerFactory factory = Persistence
				.createEntityManagerFactory("mengya");
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		Teacher teacher = new Teacher(" ");
		Student stu = new Student(" ");
		//  
		teacher.addStudent(stu);
		em.persist(stu);
		em.persist(teacher);
		em.getTransaction().commit();
		em.close();
		factory.close();
	}

	/**
	 *  
	 * 
	 */
	@Test
	public void removeTeacherStudent() {
		EntityManagerFactory factory = Persistence
				.createEntityManagerFactory("mengya");
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		Teacher t = em.find(Teacher.class, 1);
		t.removeStudent(em.getReference(Student.class, 1));
		em.getTransaction().commit();
		em.close();
		factory.close();
	}

	@Test
	public void deleteStudentTeacher() {
		EntityManagerFactory factory = Persistence
				.createEntityManagerFactory("mengya");
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		em.remove(em.getReference(Student.class, 1));
		em.remove(em.getReference(Teacher.class, 1));
		em.getTransaction().commit();
		em.close();
		factory.close();
	}

	//  

	@Test
	public void deleteTeacherStudent() {
		EntityManagerFactory factory = Persistence
				.createEntityManagerFactory("mengya");
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		em.remove(em.getReference(Teacher.class, 2));
		em.remove(em.getReference(Student.class, 2));
		em.getTransaction().commit();
		em.close();
		factory.close();
	}

	//  

	/**
	 *  , 
	 * 
	 */
	@Test
	public void deleteStudent() {
		EntityManagerFactory factory = Persistence
				.createEntityManagerFactory("mengya");
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		em.remove(em.getReference(Student.class, 3));
		em.getTransaction().commit();
		em.close();
		factory.close();
	}

	/**
	 *  , 
	 * 
	 */
	@Test
	public void deleteTeacher() {
		EntityManagerFactory factory = Persistence
				.createEntityManagerFactory("mengya");
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		em.remove(em.getReference(Teacher.class, 3));
		em.getTransaction().commit();
		em.close();
		factory.close();
	}

}