按计数在Spring MVC中排序,用于OneToMany变量

huangapple 未分类评论59阅读模式
英文:

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 = &quot;events&quot;)
public class Event {

	@Id
	@GeneratedValue
	private long id;

	@ManyToOne
	@JoinColumn(name = &quot;venue_fk&quot;)
	private Venue venue;
    // Setters, getters...
}

and

@Entity
@Table(name = &quot;venues&quot;)
public class Venue {
	@Id
	@GeneratedValue
	private long id;

    private String name;

	@OneToMany
	private Set&lt;Event&gt; events = new HashSet&lt;&gt;();
    // Setters, getters...
}

The thing I want to do is to write a method in VenueService that returns Iterable&lt;Venue&gt; 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&lt;Venue&gt; 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.

huangapple
  • 本文由 发表于 2020年3月15日 06:45:12
  • 转载请务必保留本文链接:https://java.coder-hub.com/60688007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定