Play Framework joins and the model

2019-08-31 09:56发布

This isn't a problem, its more of a "is this the right approach" kind of deal.

So lets say I have a model like this

class A extends Model{
    @OneToMany(cascade = CascadeType.ALL)
    public B;
}

class B extends Model{

   String c;
}

Now I want to access all objects of A that have a particular value of c in their B objects.

So should I:

  1. Get all objects of B with a certain value c and then find corresponding A's with those objects(if so how, getting confused)
  2. Get all objects of A with find.all() then look through the list (seems a bad idea as there will be a large amount of A's and not so many B's).

Any help would be appreciated (oh and assume I've written @Entity and @Required and all the rest appropriately)

1条回答
The star\"
2楼-- · 2019-08-31 10:41

Option 1 is the right way to go. You can use a query such as

A.find().where().eq("b.id", yourBId).findList();

Possibly adding a fetch("b"), if it isn't fetched automatically.

查看更多
登录 后发表回答