How can I sort related objects in Ebean?

I am using Play Framework and Ebean ORM. Say I have 2 related entity classes (Card.java and FinalMark.java)

Card.java

@Entity public class Card extends Model { private static final long serialVersionUID = 1L; @Id . . . @OneToMany(mappedBy = "card") public List<FinalMark> finalMarks; . . . public static Finder<Integer, Card> find = new Finder<>(Integer.class, Card.class); } 

FinalMark.java

 @Entity public class FinalMark extends Model { private static final long serialVersionUID = 1L; @Id @ManyToOne public Card card; . . . public static Finder<Integer, FinalMark> find = new Finder<>(Integer.class, FinalMark.class); } 

When I retrieve a Card instance (e.g. Card.find.byId ()), all related FinalMark instances FinalMark also be retrieved. But how can I sort them? Is it possible for Ebean or should I sort the resulting list?

Thanks for your time.

+7
source share
2 answers

You can add the @javax.persistence.OrderBy annotation to the @OneToMany field.

For example, if you have a position on your FinalMark object, you can order this code in this field:

 @Entity public class Card extends Model { ... @OneToMany(mappedBy = "card") @OrderBy("position asc") public List<FinalMark> finalMarks; ... } 
+7
source share

Use this

Card.find.byId().orderBy("id asc"); // this will give a sorted order by id (entity)

+3
source share

All Articles