【发布时间】:2021-09-23 11:39:35
【问题描述】:
我正在构建一个应用程序,该应用程序具有学生、课程数据库,并且它还跟踪每个学生正在学习的所有课程,
我有一个 Course 和 Student 实体,它们与实体 StudentCourses 和一个可嵌入的 StudentCoursesID 类具有一对多的关系。
在 StudentCourse 中,当我尝试使用注释 AssociationOverride 时,它给了我一个问题 “无法解析持久类型的覆盖属性“学生”” “无法解决持续类型的覆盖属性“课程”” 和 "嵌入式 ID 类不应包含关系映射"
我不明白我是否做错了映射,或者我的实体和类之间是否存在断开连接。 下面我有每个以 StudentCourses 开头的实体,这给了我一些问题。
学生课程
package jpa.entitymodels;
import javax.persistence.*;
@Entity
@Table(name = "student_courses")
@AssociationOverrides({
@AssociationOverride(name = "student", joinColumns = @JoinColumn(name = "sEmail")),
@AssociationOverride(name = "course", joinColumns = @JoinColumn(name = "cId"))
})
public class StudentCourses {
private StudentCoursesId id = new StudentCoursesId();
public StudentCourses() {
}
public StudentCourses(StudentCoursesId id) {
this.id = id;
}
@EmbeddedId
public StudentCoursesId getId() {
return id;
}
学生课程编号
package jpa.entitymodels;
import javax.persistence.Embeddable;
import javax.persistence.ManyToOne;
import java.io.Serializable;
@Embeddable
public class StudentCoursesId implements Serializable {
private static final long serialVersionUID = 1L;
private Student student;
private Course course;
public StudentCoursesId() {
}
@ManyToOne
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
@ManyToOne
public Course getCourse() {
return course;
}
课程
package jpa.entitymodels;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@Table(name = "course")
public class Course {
@Id
@Column(name = "id")
int cId;
@Column(name = "name")
String cName;
@Column(name = "instructor")
String cInstructorName;
@OneToMany(mappedBy = "id.course", fetch = FetchType.LAZY)
List<StudentCourses> studentCourses = new ArrayList<>();
学生
package jpa.entitymodels;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
@Entity
@Table(name="student")
public class Student {
@Id
@Column(name = "email")
String sEmail;
@Column(name = "name")
String sName;
@Column(name = "password")
String sPass;
@OneToMany(mappedBy = "id.student", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
List<StudentCourses> studentCourses = new ArrayList<>();
【问题讨论】:
标签: orm mapping eclipselink one-to-many