1
我有兩個類Course
和Student
。 Student
類別使用firstName
和lastName
作爲組合鍵。我想在Course
類中使用@OrderBy("firstName ASC")
,但是有一個錯誤「找不到來自@OrderBy子句的屬性:Student.firstName」。如何使用@OrderBy對Hibernate中的某個複合鍵進行排序
如何排序其中一個組合鍵(例如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;
.....
}
它不是由拼寫錯誤造成的,我糾正它,但仍然得到錯誤。看起來hibernate將複合鍵實現爲一個內部字段,並且firstName對於Student不再是有效的字段。如果這是真的,有沒有辦法按firstName排序? – hebingliu
默認是按主鍵{firstName,lastName}排序,但我只需要按firstName排序。 – hebingliu
@ hebingliu:相應地更新了我的帖子。 –