hibernate 多对多 配置

1. 创建表Teacher和Student和中间表teacher_student

 

CREATE TABLE `teacher` (               


              `tid` int(11) NOT NULL auto_increment,                 


              `name` varchar(50) NOT NULL, 


              PRIMARY KEY  (`tid`)                    


            ) ENGINE=InnoDB DEFAULT CHARSET=utf8;            





CREATE TABLE `student` (               


              `sid` int(11) NOT NULL auto_increment,                 


              `name` varchar(50) NOT NULL, 


              PRIMARY KEY  (`sid`)                    


            ) ENGINE=InnoDB DEFAULT CHARSET=utf8;            





create table teacher_student(


       tid int(11) not null,       


       sid int(11) not null


)ENGINE=InnoDB DEFAULT CHARSET=utf8;   
 

 

2. 创建实体类 Teacher 和 Student

public class Student implements java.io.Serializable {

	// Fields

	private Integer sid;
	private String name;

	private Set<Teacher> teachers;
	// Constructors

	public Set<Teacher> getTeachers() {
		return teachers;
	}

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

	/** default constructor */
	public Student() {
	}

	/** full constructor */
	public Student(String name) {
		this.name = name;
	}

	// Property accessors

	public Integer getSid() {
		return this.sid;
	}

	public void setSid(Integer sid) {
		this.sid = sid;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

}
 
public class Teacher implements java.io.Serializable {

	// Fields

	private Integer tid;
	private String name;
	
	private Set<Student> stus;
	
	// Constructors

	public Set<Student> getStus() {
		return stus;
	}

	public void setStus(Set<Student> stus) {
		this.stus = stus;
	}

	/** default constructor */
	public Teacher() {
	}

	/** full constructor */
	public Teacher(String name) {
		this.name = name;
	}

	// Property accessors

	public Integer getTid() {
		return this.tid;
	}

	public void setTid(Integer tid) {
		this.tid = tid;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

}
 

 

3. 配置Teacher.hbm.xml 和 student.hbm.xml配置文件

 

 

Teacher.hbm.xml

<hibernate-mapping>
    <class name="ssh.pojo.Teacher" table="teacher" catalog="zf">
        <id name="tid" type="java.lang.Integer">
            <column name="tid" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" length="50" not-null="true" />
        </property>
        <set name="stus" table="teacher_student" cascade="save-update" inverse="true">
        	<key column="tid"></key>
        	<many-to-many column="sid" class="ssh.pojo.Student"></many-to-many>
        </set>
    </class>
</hibernate-mapping>

 

Student.hbm.xml

<hibernate-mapping>
    <class name="ssh.pojo.Student" table="student" catalog="zf">
        <id name="sid" type="java.lang.Integer">
            <column name="sid" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" length="50" not-null="true" />
        </property>
        <set name="teachers" table="teacher_student" cascade="save-update">
        	<key column="sid"></key>
        	<many-to-many column="tid" class="ssh.pojo.Teacher"></many-to-many>
        </set>
    </class>
</hibernate-mapping>
 

 

 

4. 测试

 

Session session = null;  // 一级缓存
		TblUser user = null;
		try{
			session = HibernateSessionFactory.getSession();
			session.beginTransaction();
			
			Set<Teacher> teachers = new HashSet<Teacher>();
			Set<Student> students = new HashSet<Student>();
			// 初始化老师
			Teacher tWang = new Teacher();
			tWang.setName("王");
			Teacher tZhang = new Teacher();
			tZhang.setName("张");
			teachers.add(tWang);
			teachers.add(tZhang);
			// 初始化学生
			Student s1 = new Student();
			Student s2 = new Student();
			Student s3 = new Student();
			s1.setName("刘另一");
			s2.setName("任汉卿");
			s3.setName("付博文");
			students.add(s1);
			students.add(s2);
			students.add(s3);
			
			// teacher--->stu
			tWang.setStus(students);
			tZhang.setStus(students);
			// stu---->teacher
			s1.setTeachers(teachers);
			s2.setTeachers(teachers);
			s3.setTeachers(teachers);
			
			// save to db
			session.save(tWang);
			session.save(tZhang);
			session.save(s1);
			session.save(s2);
			session.save(s3);
			
			
			session.getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			session.getTransaction().rollback();
		}
结果:
Hibernate: insert into zf.teacher (name) values (?)
Hibernate: insert into zf.student (name) values (?)
Hibernate: insert into zf.teacher (name) values (?)
Hibernate: insert into zf.student (name) values (?)
Hibernate: insert into zf.student (name) values (?)
Hibernate: insert into teacher_student (sid, tid) values (?, ?)
Hibernate: insert into teacher_student (sid, tid) values (?, ?)
Hibernate: insert into teacher_student (sid, tid) values (?, ?)
Hibernate: insert into teacher_student (sid, tid) values (?, ?)
Hibernate: insert into teacher_student (sid, tid) values (?, ?)
Hibernate: insert into teacher_student (sid, tid) values (?, ?)
 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值