在 hibernate 中使用 where 子句自定义特定字段的映射

问题描述 投票:0回答:1

我有实体类

Course 
带有一些字段:

    @Id
    Long id;

    @OneToMany(mappedBy = "course_id", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    List<CourseRelationship> preRequisiteCourses;

    @OneToMany(mappedBy = "course_id", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    List<CourseRelationship> preCourses;

    @OneToMany(mappedBy = "course_id", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    List<CourseRelationship> coreQuisiteCourses;

还有实体类

CourseRelationShip
:

    @Id
    Long id;

    @Column(name = "course_id")
    Long courseId;

    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "course_id", insertable = false, updatable = false)
    Course course;

    @Column(name = "course_constraint_id")
    Long courseConstraintId;

    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "course_constraint_id", insertable = false, updatable = false)
    Course courseConstraint;

    @Enumerated(value = EnumType.STRING)
    Relation relation;
    public enum Relation {
        PREREQUISITE,
        PRECOURSE,
        COREQUISITECOURSE
    }

我想通过添加以下内容来获取预备课程:

@Where(clause = "relation = 'PRECOURSE'")
List<CourseRelationship> preCourses;

之后,我会将

List<CourseRelationship>
转换为
List<Course>

但是 @Where 注释已弃用并标记为删除。有没有任何解决方案,例如添加@ManyToMany或@JoinTable。

您的回答将不胜感激。

java mysql spring-boot hibernate
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.