英文:
Sort by count in spring mvc, for a OneToMany variable
问题
以下是翻译好的内容:
我在我的 Spring 应用程序中有两个类:
@Entity
@Table(name = "events")
public class Event {
@Id
@GeneratedValue
private long id;
@ManyToOne
@JoinColumn(name = "venue_fk")
private Venue venue;
// 设置器,获取器...
}
以及
@Entity
@Table(name = "venues")
public class Venue {
@Id
@GeneratedValue
private long id;
private String name;
@OneToMany
private Set<Event> events = new HashSet<>();
// 设置器,获取器...
}
我想要做的事情是在 VenueService
中编写一个方法,返回 Iterable<Venue>
,并且根据它们拥有的事件数量(降序)和场馆名称(升序)在这个可迭代对象中呈现场馆对象,拥有更多事件的场馆会优先出现。
例如,我有三个场馆,venueA 注册了 3 个事件,venueB 注册了 3 个事件,而 venueC 注册了 4 个事件,那么方法 findAllVenuesByNoOfEvents()
应该返回按顺序排列的 Iterable<Venue>
,顺序为 venueC、venueA、venueB。
我对 Spring 还很陌生,真的不知道如何做这个,提前感谢您的帮助。
英文:
I have two classes in my spring application:
@Entity
@Table(name = "events")
public class Event {
@Id
@GeneratedValue
private long id;
@ManyToOne
@JoinColumn(name = "venue_fk")
private Venue venue;
// Setters, getters...
}
and
@Entity
@Table(name = "venues")
public class Venue {
@Id
@GeneratedValue
private long id;
private String name;
@OneToMany
private Set<Event> events = new HashSet<>();
// Setters, getters...
}
The thing I want to do is to write a method in VenueService that returns Iterable<Venue>
and venue objects appear in this Iterable based on how many events they have (DESC) and then venue name(ASC), the more events one venue has, the earlier it appears.
E.g. I have three venues, venueA has 3 events registered with it, venueB has 3 events registered with it, and venueC has 4 events registered with it, then the method findAllVenuesByNoOfEvents()
should return Iterable<Venue>
in the order venueC, venueA, venueB.
I'm new to spring and I'm really don't know how to do this, thanks in advance.
专注分享java语言的经验与见解,让所有开发者获益!
评论