Hi,
I have a problem with getting List<Note> from entity Person using Spring data JPA specifications (because of pagination). I need to get all notes by person but dependency between these two entities is on Person side. I don't know how to create my Predicate because Note doesn't contain any attribute related to Person.
I simply can get List<Note> with Persons getter but i can't use this way because i need returned data paginated. I am using findAll(Specifications,Pageable).
I'm sure that Specifications are able to create query what I want, but I don't know how.Normally, I would write something like this, but i don't have an attribute person in Note and database can't be remapped at this stage.Code:@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; }
Thank you,Code: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


Reply With Quote
