hibernate多对多映射文件配置位置
当两个实体是多对多联系时,联系集有一个多int型字段,如何在hibernate中设置。比如:createtablestudent(idintprimarykey,name...
当两个实体是多对多联系时,联系集有一个多int型字段,如何在hibernate中设置。
比如:
create table student
(
id int primary key,
name varchar(10),
)
create table course
(
id int primary key,
name varchar(10),
)
create table student_course
(
studentId int references student(id),
courseId int references course(id),
score int, ------成绩
primary key (studentId, courseId),
) 展开
比如:
create table student
(
id int primary key,
name varchar(10),
)
create table course
(
id int primary key,
name varchar(10),
)
create table student_course
(
studentId int references student(id),
courseId int references course(id),
score int, ------成绩
primary key (studentId, courseId),
) 展开
展开全部
采用JPA标注实体类挺简单的
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
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;
@Entity
@Table(name="student")
public class Student {
private int id;
private String name;
private Set<Course> courses=new HashSet<Course>();
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="sname")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToMany(cascade={CascadeType.ALL})
@JoinTable(name="student_course",
joinColumns={@JoinColumn(name="student_id")},
inverseJoinColumns={@JoinColumn(name="course_id")}
)
public Set<Course> getCourses() {
return courses;
}
public void setCourses(Set<Course> courses) {
this.courses = courses;
}
}
Course 类:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="course")
public class Course {
private int id;
private String name;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="cname")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="student_course")
public class Score {
private int score;
private Student student;
private Course course;
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="student_id")
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="course_id")
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
}
如果想采用XML配置的话,请参考Hiberante的官方文档,里面有详细的配置
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
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;
@Entity
@Table(name="student")
public class Student {
private int id;
private String name;
private Set<Course> courses=new HashSet<Course>();
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="sname")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToMany(cascade={CascadeType.ALL})
@JoinTable(name="student_course",
joinColumns={@JoinColumn(name="student_id")},
inverseJoinColumns={@JoinColumn(name="course_id")}
)
public Set<Course> getCourses() {
return courses;
}
public void setCourses(Set<Course> courses) {
this.courses = courses;
}
}
Course 类:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="course")
public class Course {
private int id;
private String name;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="cname")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="student_course")
public class Score {
private int score;
private Student student;
private Course course;
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="student_id")
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="course_id")
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
}
如果想采用XML配置的话,请参考Hiberante的官方文档,里面有详细的配置
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你想表达什么意思? 多多多关联 用 3张表 中间表用联合主键就行
映射文件是对 实体的映射 用 Many-To-Many
映射文件是对 实体的映射 用 Many-To-Many
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询