如何使用 @OrderBy 对 Hibernate 中的复合键之一进行排序

发布于 2024-11-18 11:51:31 字数 813 浏览 3 评论 0原文

我有两个课程 CourseStudentStudent 类使用 firstNamelastName 作为复合键。我想在 Course 类中使用 @OrderBy("firstName ASC"),但出现错误“未找到 @OrderBy 子句的属性:Student.firstName “

如何对其中一个组合键(例如 firstName)进行排序?

public class Course{
    @OneToMany(mappedBy="course")
    @OrderBy("firstName ASC")  
    // Error: property from @OrderBy clause not found: Student.firstName, why?
    private List<Student> students;
.....
}


public class Student{
    @Id
    @Column(name="first_name")
    private String firtName;
    @Id
    @Column(name="last_name")
    private String lastName;

    @ManyToOne
    @JoinColumn(name="course_id")
    private Course course;
.....
}

I have two classes Course and Student. Student class use firstName and lastName as the composite-key. I want to use @OrderBy("firstName ASC") in Course class, but there is an error "property from @OrderBy clause not found: Student.firstName".

How can I sort on one of the composite keys (such as firstName)?

public class Course{
    @OneToMany(mappedBy="course")
    @OrderBy("firstName ASC")  
    // Error: property from @OrderBy clause not found: Student.firstName, why?
    private List<Student> students;
.....
}


public class Student{
    @Id
    @Column(name="first_name")
    private String firtName;
    @Id
    @Column(name="last_name")
    private String lastName;

    @ManyToOne
    @JoinColumn(name="course_id")
    private Course course;
.....
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你不是我要的菜∠ 2024-11-25 11:51:31

您将其拼写错误为 firtName - 注意 s 丢失了。解决这个问题,很可能一切都会好起来的。

[已编辑]

以防万一它仍然无法正常工作。尝试将其替换为 @EmbeddedId。如下所示,

public class Student implements Serializable{
    @EmbeddedId
    private StudentPK name;

    @ManyToOne
    @JoinColumn(name="course_id")
    private Course course;
    .....

    @Embeddable
    public static class StudentPK implements Serializable {
        @Column(name="first_name")
        private String firtName;

        @Column(name="last_name")
        private String lastName;

        ....
    }
}

那么它应该可以使用,

public class Course{
    @OneToMany(mappedBy="course")
    @OrderBy("name.firstName ASC")  
    private List students;
    .....
}

You misspelled it to, firtName -- notice s is missing. Fix that and things will be fine, most likely.

[Edited]

In case it is still not working. Try to replace this by @EmbeddedId, instead. Like below,

public class Student implements Serializable{
    @EmbeddedId
    private StudentPK name;

    @ManyToOne
    @JoinColumn(name="course_id")
    private Course course;
    .....

    @Embeddable
    public static class StudentPK implements Serializable {
        @Column(name="first_name")
        private String firtName;

        @Column(name="last_name")
        private String lastName;

        ....
    }
}

Then it should work using,

public class Course{
    @OneToMany(mappedBy="course")
    @OrderBy("name.firstName ASC")  
    private List students;
    .....
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文