Hibernate Many-to-Many

  • Post author:
  • Post category:其他



学生实体

package com.hibernate.ano.mant_to_many;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
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;

/**
 * @ClassName: Student.java
 *
 * @Description: 学生
 *
 * @author GERRARD
 *
 * @date 2015年1月27日下午1:55:30
 * 
 */
@Entity
@Table(name = "student")
public class Student {
	

	/**
	 * 主键ID
	 */
	private String pid;
	
	/**
	 * 姓名
	 */
	private String studentName;
	
	/**
	 * 选修课程
	 */
	private Set<Course> course;

	public Student() {

	}

	public Student(String pid, String studentName, Set<Course> course) {
		this.pid = pid;
		this.studentName = studentName;
		this.course = course;
	}

	@Id
    @Column(name = "pid", unique = true, nullable = false, length = 32)
    @GeneratedValue(generator = "generator")
    @GenericGenerator(name = "generator", strategy = "uuid")
	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	@Column(name = "student_name", length = 64)
	public String getStudentName() {
		return studentName;
	}

	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}

	/**
	 * Hibernate 会自动创建一张关系表stu_cou, 里边有俩字段stu_id和cou_id
	 * @return
	 */
	@ManyToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE, 
            CascadeType.REFRESH}, fetch=FetchType.EAGER)
    @JoinTable(name = "stu_cou", joinColumns = { @JoinColumn(name = "stu_id") }, inverseJoinColumns = { @JoinColumn(name = "cou_id") })
	public Set<Course> getCourse() {
		return course;
	}

	public void setCourse(Set<Course> course) {
		this.course = course;
	}

}


课程实体

package com.hibernate.ano.mant_to_many;

import java.util.Set;

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

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

/**
 * @ClassName: Classz.java
 *
 * @Description: 课程
 *
 * @author GERRARD
 *
 * @date 2015年1月27日下午1:50:07
 * 
 */
@Entity
@Table(name = "course")
public class Course implements java.io.Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 6398143635533582335L;
	
	/**
	 * 主键ID
	 */
	private String pid;
	
	/**
	 * 课程名
	 */
	private String courseName;
	
	/**
	 * 选修课程学生
	 */
	private Set<Student> Student;

	public Course() {

	}

	public Course(String pid, String courseName,
			Set<Student> student) {
		this.pid = pid;
		this.courseName = courseName;
		this.Student = student;
	}

	@Id
    @Column(name = "pid", unique = true, nullable = false, length = 32)
    @GeneratedValue(generator = "generator")
    @GenericGenerator(name = "generator", strategy = "uuid")
	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	@Column(name = "course_name", length = 64)
	public String getCourseName() {
		return courseName;
	}

	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}

	@ManyToMany(fetch = FetchType.LAZY, mappedBy = "course")
    @NotFound(action = NotFoundAction.IGNORE)
	public Set<Student> getStudent() {
		return Student;
	}

	public void setStudent(Set<Student> student) {
		Student = student;
	}

}



版权声明:本文为delongcpp原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。