hibernate关联映射中的多对多



*****************

Teacher.java

****************

package blog.hibernate.domain;

import java.util.Set;

public class Teacher {
	private int id;
	private String name;
	private Set<Student> students;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Set<Student> getStudents() {
		return students;
	}
	public void setStudents(Set<Student> students) {
		this.students = students;
	}	
}

******************

Student.hbm.xml

*****************

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="blog.hibernate.domain">
	<class name="Student" table="students">
		<id name="id" column="student_id">
			<generator class="native"></generator>
		</id>
		<property name="name" column="student_name"></property>
		
		<set name="teachers" table="teacher_student">
			<key column="student_id"></key>
			<many-to-many class="Teacher" column="teacher_id"></many-to-many>
		</set>
	</class>
</hibernate-mapping>



******************

Student.java

*****************

package blog.hibernate.domain;

import java.util.Set;

public class Student {
	private int id;
	private String name;
	private Set<Teacher> teachers;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Set<Teacher> getTeachers() {
		return teachers;
	}
	public void setTeachers(Set<Teacher> teachers) {
		this.teachers = teachers;
	}	
}

******************

Teacher.hbm.xml

*****************

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="blog.hibernate.domain">
	<class name="Teacher" table="teachers">
		<id name="id" column="teacher_id">
			<generator class="native"></generator>
		</id>
		<property name="name" column="teacher_name"></property>
		
		<set name="students" table="teacher_student">
			<key column="teacher_id"></key>
			<many-to-many class="Student" column="student_id"></many-to-many>
		</set>
	</class>
</hibernate-mapping>

**************

juint test : Many2Many.java

*************

package junit.test;


import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.BeforeClass;
import org.junit.Test;

import blog.hibernate.HibernateUtil;
import blog.hibernate.domain.Student;
import blog.hibernate.domain.Teacher;

public class Many2Many {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}
	
	@Test
	public void add(){
		Teacher t1 = new Teacher();
		t1.setName("t1");
		
		Teacher t2 = new Teacher();
		t2.setName("t2");
		
		Set<Teacher> ts = new HashSet<Teacher>();
		ts.add(t1);
		ts.add(t1);
		
		Student s1 = new Student();
		s1.setName("s1");
		
		Student s2 = new Student();
		s2.setName("s2");
		
		Set<Student> ss = new HashSet<Student>();
		ss.add(s1);
		ss.add(s2);
		
		t1.setStudents(ss);
		t2.setStudents(ss);
		
		/*与上面的有一方面设置就行,全设置会导致org.hibernate.exception.ConstraintViolationException
		s1.setTeachers(ts);
		s2.setTeachers(ts);*/
		
		
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			session.save(t1);
			session.save(t2);
			session.save(s1);
			session.save(s2);
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		}finally{
			if (session != null) {
				session.close();
			}
		}		
	}
}



转载于:https://www.cnblogs.com/xzf007/archive/2012/08/20/2873822.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值