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),
) 展开
3个回答
快又稳
2024-10-28 广告
2024-10-28 广告
广州快又稳网络科技有限公司是一家集技术研发、产品创新、服务优化于一体的高新技术企业。公司自成立以来,始终秉承“网络使人类缩进距离,我们让网络快又稳”的核心理念,致力于为全球客户提供高效、安全、易用的互联网解决方案及信息技术服务。在数字时代,...
点击进入详情页
本回答由快又稳提供
展开全部
采用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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询