开发者

Is it possible to have Hibernate perform an OrderBy through a ManyToMany relationship?

I have inherited a code base with some models and a request to implement sorting them. As I'm not a hibernate guru, I'm stumped.

(Pardon the Pseudo Code)

class Event开发者_C百科 {
    @OneToMany(mappedBy="event", fetch=FetchType.LAZY)
    @Cascade({CascadeType.ALL, CascadeType.DELETE_ORPHAN})
    private List<Attendee> attendees = new ArrayList<Attendee>(50);
}

class Attendee {
    @ManyToOne
    @JoinColumn(name="event_id")
    private Event event;

    @ManyToOne
    @JoinColumn(name="person")
    private Person person;
}

class Person {
    private string lastName;
    private string firstName;
}

I would like to be able to sort the Event's Attendee list by the lastname/firstname of the Person, but I don't see how to do it with the table in the middle. I've found many online references that would work wonderfully w/o the middle table. I've tried using (in the Event class):

@OrderBy("attendee.person.lastName") 
@OrderBy("person.lastName")

and in the Attendee class by:

@OrderBy("attendee.lastName")
@OrderBy("lastName")

without any luck. Does anyone have any suggestions or references that might have insight? I can post actual details if that'd help or if any questions help dig deeper.


I think the only ways are

  • to execute a dedicated HQL request to get all the attendees of an event: select a from Attendee a inner join fetch a.person p where a.event = :event order by p.lastName, p.firstName
  • to have a method in Event which returns a copy of the list of attendeed, sorted using a dedicated Comparator.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜