I have a ManyToMany which I have mapped like this question.
Please note, I have removed boilerplate for simplicity
@Entity
class Person {
@OneToMany(mappedBy = "person")
@OrderBy("sort")
private List<PersonAddress> adresses = new ArrayList<>();
}
@Entity
class PersonAdress {
@EmbeddedId
private PersonAdressId id;
@Column
private int sort;
@ManyToOne
private Person person;
@ManyToOne
private Address address;
}
@Entity
class Address {
@OneToMany(mappedBy = "address")
@OrderBy("sort")
private List<PersonAddress> persons = new ArrayList<>();
}
@Embeddable
public class PersonAdressId implements Serializable {
@Column(name = "person_id")
private long personId;
@Column(name = "address_id")
private long addressId;
}
I am trying to get all the adresses for person, and order by the sort attribute. But for some reason I get exception or I don't get it sorted.
I have tried the following:
"select p from Person p where p.id=pid join fetch p.address a order by a.sort"
I have also tried:
Person person = entityManager.find(Person.class, personId);
person.getAddress() //<-- This should use the @OrderBy, but I don't get it ordered nor does it print out order by in the output
Can anyone spot why its not working?