Ebean ManyToMany查询 [英] Ebean ManyToMany query
问题描述
我有两个课程,用户和汽车。两者都有ManyToMany相互映射。
I have two classes, user and car. Both have ManyToMany mapping to each other.
用户:
@Entity
public class User extends Model {
private int year;
@ManyToMany(cascade=CascadeType.ALL)
private List<Car> cars;
}
汽车:
@Entity
public class Car extends Model {
@ManyToMany(mappedBy = "cars", cascade=CascadeType.ALL )
private List<User> users;
}
使用ebean,我只想查询那些从1999年开始的汽车在列表中提供用户。我不想在Java代码中迭代用户的汽车列表。
Using ebean, I would like to query only those cars from year 1999 that have give user in their list. I do not want to iterate over the user's car list in Java code.
我没有找到任何文档,多少对多的查询应该是什么样子。所以我想这样:
I didn't find any documentation how many-to-many queries should look like. So I would something like this:
public List<Car> findCars(int year, User user) {
return Car.find.where().eq("year", int).eq("users", user).findList();
}
这是否可以使用Ebean?
Is this possible with Ebean?
推荐答案
检查类似的提问(和回答)
最有可能你的取景器应该是这样的:
Most probably your finder should look like:
public List<Car> findCars(int year, User user) {
return find.where().eq("year", year).eq("users.id", user.id).findList();
}
BTW我假设你有一些 id
字段,但只是没有告诉我们。同时公开您的字段,因此您不需要为每个字段编写getter / setter。
BTW I assume that you have some id
field but just didn't show us. Also make your fields public, so you won't need to write getters/setters for each.
这篇关于Ebean ManyToMany查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!