论坛首页 Java企业应用论坛

hibernate多对多单向(双向)关系映射

浏览 1798 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2013-03-21  

 

1.多对多单向关系映射(学生知道自己要上的课程,课程不知道有哪些学生上自己)

package yingjun.model;

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.JoinColumns;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
@Entity
public class Student {
	private int sid;
	private String name;
	private Set<Course> courses=new HashSet<Course>();
	@Id
	@GeneratedValue
	public int getSid() {
		return sid;
	}
	public void setSid(int sid) {
		this.sid = sid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@ManyToMany
	@JoinTable(name="s_c",
			  joinColumns={@JoinColumn(name="student_id")},  //指向student表中的外键
			  inverseJoinColumns={@JoinColumn(name="course_id")} //指向course表中的外键
			
	)
	public Set<Course> getCourses() {
		return courses;
	}
	public void setCourses(Set<Course> courses) {
		this.courses = courses;
	}
	
}

 

package yingjun.model;

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

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Course {
	private int cid;
	private String name;
	
	@Id
	@GeneratedValue
	public int getCid() {
		return cid;
	}
	public void setCid(int cid) {
		this.cid = cid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

}

 

 create table Course (
        cid integer not null auto_increment,
        name varchar(255),
        primary key (cid)
    )

    create table Student (
        sid integer not null auto_increment,
        name varchar(255),
        primary key (sid)
    )

    create table s_c (
        student_id integer not null,
        course_id integer not null,
        primary key (student_id, course_id)
    )

    alter table s_c 
        add index FK1BB977CE40B3 (course_id), 
        add constraint FK1BB977CE40B3 
        foreign key (course_id) 
        references Course (cid)

    alter table s_c 
        add index FK1BB9776D99621 (student_id), 
        add constraint FK1BB9776D99621 
        foreign key (student_id) 
        references Student (sid)

 1.多对多双向关系映射(学生知道自己要上的课程,课程也知道有哪些学生上自己

package yingjun.model;

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;
@Entity
public class Student {
	private int sid;
	private String name;
	private Set<Course> courses=new HashSet<Course>();
	@Id
	@GeneratedValue
	public int getSid() {
		return sid;
	}
	public void setSid(int sid) {
		this.sid = sid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@ManyToMany
	@JoinTable(name="s_c",
			  joinColumns={@JoinColumn(name="student_id")},  //指向student表中的外键
			  inverseJoinColumns={@JoinColumn(name="course_id")} //指向course表中的外键
			
	)
	public Set<Course> getCourses() {
		return courses;
	}
	public void setCourses(Set<Course> courses) {
		this.courses = courses;
	}
	
}

 

package yingjun.model;

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 Course {
	private int cid;
	private String name;
	private Set<Student> students=new HashSet<Student>();
	
	@Id
	@GeneratedValue
	public int getCid() {
		return cid;
	}
	public void setCid(int cid) {
		this.cid = cid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setStudents(Set<Student> students) {
		this.students = students;
	}
	@ManyToMany(mappedBy="courses")
	public Set<Student> getStudents() {
		return students;
	}

}

 生成的建表语句和多对多单向关联的一样(在数据库他们的表现一样)

 

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics