我在使用Spring data JPA规范获取实体Person的List时遇到了问题(由于分页)。我需要通过person获取所有notes,但这两个实体之间的依赖关系在Person方面。我不知道如何创建我的Predicate,因为Note不包含与Person相关的任何属性。
我可以简单地使用Persons getter获取List,但我无法使用此方法,因为我需要返回分页数据。
@Entity
public class Person implements Serializable {
@Id
private Long personId;
@OneToMany
@JoinColumn(name = "personId")
private List<Note> notes;
}
@Entity
public class Note implements Serializable {
@Id
private Long noteId;
}
通常情况下,我会像这样写,但是在“Note”中没有“person”属性,且数据库无法在此阶段被重新映射。
public static Specification<Note> notesByPerson(final Long personId) {
return new Specification<Note>() {
@Override
public Predicate toPredicate(final Root<Note> root, final CriteriaQuery<?> query,
final CriteriaBuilder builder) {
final Path<Person> per = root.<Person> get("person");
return builder.equal(per.<Long> get("personId"), personId);
}
};
}
谢谢,Zdend。
@ManyToOne Person person;
,这样你就可以进行简单的Join操作了。 - rwyland