2

I'm following How can I sort related entities in Ebean? but i got this error

 RuntimeException: Can not find Master [class models.Card] 
 in Child[models.FinalMark]    

Is the solution forgot to put this? Even with ID stil not working.

@Id
public Integer id;

I think my case quite similar.

@Entity
public class Club extends Model {
    @Id
    public Long id;
    public String name;

    @OneToMany(cascade = CascadeType.ALL)
    @OrderBy("name asc")
    public List<Male> male = new ArrayList<>();

    @OneToMany(cascade = CascadeType.ALL)
    @OrderBy("name asc")
    public List<Female> female = new ArrayList<>();

    private static Finder<Long, Club> find = 
    new Finder<>(Long.class, Club.class);
}

@Entity
public class Male extends Model {
  @Id
  public Long id;
  public String name;
  private static Finder<Long, Male> find = new Finder<>(Long.class, Male.class);
}

@Entity
public class Female extends Model {
  @Id
  public Long id;
  @ManyToOne
  public Club club;
  public String name;
  private static Finder<Long, Female> find = new Finder<>(Long.class, Female.class);
}

I've intial data

male:
- !!models.Male &m1
    name:           "Martin"

- !!models.Male &m2
    name:           "Alan"

female:
- !!models.Female &f1
    name:           "June"

- !!models.Female &f2
    name:           "Babe"


club:
- !!models.Club
    name:           "BigClub"
    male:
    - *m1
    - *m2
    female:
    - *f1
    - *f2

during initialization.

    Map<String, List<Object>> all = (Map<String, List<Object>>) Yaml
                    .load("initial-data.yml");

   // Insert club
   Ebean.save(all.get("club"));

when i call Club.find.all(), the male and female fields still not sort in ascending. I've tried 2 different ways (each in male & female). both also not sort in asc. any idea?

Community
  • 1
  • 1
keepscoding
  • 146
  • 1
  • 7

1 Answers1

0

You need to specify the "path" of the related object when you are configuring the order-by like so:

@OneToMany(cascade = CascadeType.ALL)
@OrderBy("male.name asc")
public List<Male> male = new ArrayList<>();

@OneToMany(cascade = CascadeType.ALL)
@OrderBy("female.name asc")
public List<Female> female = new ArrayList<>();

Otherwise, it thinks that you're referring to "name" field of the current class.

Atif
  • 942
  • 1
  • 6
  • 19
  • sorry, i should mentioned i'm using old ebean. After ported, the latest play-ebean in 2.4. it is working with "id asc". your suggestion is not working. Anyway, thanks. – keepscoding Sep 21 '15 at 10:02