论坛首页 Java企业应用论坛

JPA 多对多实例配置

浏览 3952 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-11-08   最后修改:2009-11-08
DAO
@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();
	}

}

 

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics