JPA中多对多的配置

以学生和老师为例
学生类:
package com.dimples.dao;

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

import javax.persistence.Column;
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.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="student")
public class Student {
	@Id
	@GenericGenerator(name="uuid",strategy="uuid")
	@GeneratedValue(generator="uuid")
	@Column(name="id")
	private String id;
	
	@Column(name="name")
	private String name;
	
	@Column(name="address")
	private String address;
	
	@ManyToMany
	@JoinTable(name="teacher_student",	//用来指定中间表的名称
				//用于指定本表在中间表的字段名称,以及中间表依赖的是本表的哪个字段
			   joinColumns= {@JoinColumn(name="student_id",referencedColumnName="id")},
				//用于指定对方表在中间表的字段名称,以及中间表依赖的是它的哪个字段
			   inverseJoinColumns= {@JoinColumn(name="teacher_id",referencedColumnName="id")}
			)
	private Set teachers = new HashSet(0);
	
	public Set getTeachers() {
		return teachers;
	}
	public void setTeachers(Set teachers) {
		this.teachers = teachers;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", address=" + address + "]";
	}
	
	

Teacher类的配置:

package com.dimples.dao;

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

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

import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="teacher")
public class Teacher {
	@Id
	@GenericGenerator(name="uuid",strategy="uuid")
	@GeneratedValue(generator="uuid")
	@Column(name="id")
	private String id;
	
	@Column(name="name")
	private String name;
	
	@Column(name="shoolName")
	private String shoolName;
	
	@Column(name="phone")
	private String phone;
	
	@ManyToMany(mappedBy="teachers")    //就配置一个mappedBy,其余的交给对方配置。
	private Set students = new HashSet(0);
	
	public Set getStudents() {
		return students;
	}
	public void setStudents(Set students) {
		this.students = students;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getShoolName() {
		return shoolName;
	}
	public void setShoolName(String shoolName) {
		this.shoolName = shoolName;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	@Override
	public String toString() {
		return "Teacher [id=" + id + ", name=" + name + ", shoolName=" + shoolName + ", phone=" + phone + "]";
	}
	
	
}

你可能感兴趣的:(Hibernate&JPA)