How can I sort related entities in Ebean?

巧了我就是萌 提交于 2019-12-10 03:59:44

问题


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 fetching Card instance (by Card.find.byId() for example), All related FinalMark instances also will be fetched. But how can I sort them? Is it possible by Ebean or I should sort resulting list?

Thank you for wasting your time.


回答1:


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

For example, if you have a position field on your FinalMark entity, you could order on that field with this code :

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



回答2:


Use this

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



来源:https://stackoverflow.com/questions/23925538/how-can-i-sort-related-entities-in-ebean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!