码迷,mamicode.com
首页 > Web开发 > 详细

hibernate--多对多双向关联(少用)

时间:2016-04-22 16:05:47      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

老师知道自己教了哪些学生, 学生也知道教自己的有哪些老师.

Teacher.java:

package com.bjsxt.hibernate;

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

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
public class Teacher {
	private int id;
	private String name;
	private Set<Student> students = new HashSet<Student>();
	@Id
	@GeneratedValue
	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;
	}
	@ManyToMany
	@JoinTable(name="t_s",
		joinColumns={@JoinColumn(name="teacher_id")},
		inverseJoinColumns={@JoinColumn(name="student_id")}
		)
	public Set<Student> getStudents() {
		return students;
	}
	public void setStudents(Set<Student> students) {
		this.students = students;
	}
}

  

Student.java:

package com.bjsxt.hibernate;

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

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

@Entity
public class Student {
	private int id;
	private String name;
	private Set<Teacher> teachers = new HashSet<Teacher>();
	@ManyToMany(mappedBy="students")
	public Set<Teacher> getTeachers() {
		return teachers;
	}
	public void setTeachers(Set<Teacher> teachers) {
		this.teachers = teachers;
	}
	@Id
	@GeneratedValue
	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;
	}
}

XML方式:

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>
	<class name="com.bjsxt.hibernate.Teacher">
		<id name="id">
			<generator class="native"></generator>
		</id>
		
		<property name="name"></property>
		<set name="students" table="t_s">
			<key column="teacher_id"></key>
			<many-to-many class="com.bjsxt.hibernate.Student" column="student_id"/>
		</set>
    </class>
	
</hibernate-mapping>

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>
	<class name="com.bjsxt.hibernate.Student">
		<id name="id">
			<generator class="native"></generator>
		</id>
		
		<property name="name"></property>
		<set name="teachers" table="t_s">
			<key column="student_id"></key>
			<many-to-many class="com.bjsxt.hibernate.Teacher" column="teacher_id"/>
		</set>
    </class>
	
</hibernate-mapping>

  

  

hibernate--多对多双向关联(少用)

标签:

原文地址:http://www.cnblogs.com/wujixing/p/5421580.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!