如何使用 @OrderBy 对 Hibernate 中的复合键之一进行排序
我有两个课程 Course
和 Student
。 Student
类使用 firstName
和 lastName
作为复合键。我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将其拼写错误为
firtName
- 注意s
丢失了。解决这个问题,很可能一切都会好起来的。[已编辑]
以防万一它仍然无法正常工作。尝试将其替换为
@EmbeddedId
。如下所示,那么它应该可以使用,
You misspelled it to,
firtName
-- notices
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,Then it should work using,