【问题标题】:ORM: EclipseLink AssociationOverride "Persistent type of override attribute cannot be resolved"ORM:EclipseLink AssociationOverride“无法解析持久类型的覆盖属性”
【发布时间】: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


    【解决方案1】:

    StudentCourses 中不存在“学生”,这就是您收到错误的原因。您要应用于可嵌入对象的覆盖需要在可嵌入映射而不是 StudentCourses 类上定义。

    @Entity
    @Table(name = "student_courses")
    public class StudentCourses {
    
      @Embedded
      @AttributesOverride({
      @AttributeOverride(name = "student", joinColumns = @JoinColumn(name = "sEmail")),
      @AttributeOverride(nname = "course", joinColumns = @JoinColumn(name = "cId"))})
      private EmbeddableClass embeddableClass;
    
      public StudentCourses() {
      }
    
    }
    

    JPA 需要一个 ID 类,其中包含简单的 pk 类型(或其他嵌套的复合 PK 类,而不是关系),要求 StudentCoursesId 具有 cId 和 sEmail 的 int/string 属性。如果您想映射到课程/学生,可以查看mapsId annotation,或者,我会这样映射:

    @Entity
    @Table(name = "student_courses")
    @IdClass(StudentCoursesId.class)
    public class StudentCourses {
    
      @Id
      @JoinColumn(name = "sEmail")
      Student student;
    
      @Id
      @JoinColumn(name = "cId")
      Course course;
    }
    
    public class StudentCoursesId implements Serializable {
    
      private static final long serialVersionUID = 1L;
    
      private String student;//Name matches id property in StudentCourses
      private int course;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-05
      • 1970-01-01
      • 1970-01-01
      • 2015-11-23
      • 2018-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多