我有一个如下所示的模型对象:
@SuppressWarnings("serial")
@Entity
@Table(name = "selections")
public class Selection extends Model {
....
@ManyToMany
private Set<Market> markets;
....
}
选择和市场都有id
属性和static Finder<Long, *> find()
方法。
我正在尝试查找包含Selection
的所有Market
个对象。
@Override @Transactional(readOnly = true) public List<Selection> findSelections(Set<Market> markets) {
// Query?
return Selection.find().where()...findList();
}
我知道我可以这样做:
return Selection.find().where().eq("markets.id", market.id).findList();
找到一个单一的市场对象 - 但是如何从Set中找到这些对象呢?没有迭代Set?
答案 0 :(得分:4)
return Selection.find().where().in("markets",markets).findList();
答案 1 :(得分:0)
我一直在努力解决同样的问题,在阅读another SO question和the Ebean's Interface Query documentation后,我发现了这个问题:
// Prepare the OQL query string
String oql = "find selection " +
"where markets.id in (:marketList) " +
"group by id " +
"having count(distinct markets.id) = :marketCount";
// Create the query
Query<Selection> query = Selection.find.setQuery(oql);
// Set the list of market IDs
List<Long> marketIds = Arrays.asList(1, 30, 9, 15, 6);
// Set query parameters (list of market IDs and how many they are)
query.setParameter("marketList", marketIds);
query.setParameter("marketCount", marketIds.size());
// Get the matching results
List<Selection> selections = query.findList();
我正在使用播放2.4.2 与 sbt-play-ebean 插件版本 2.0.0 ,我提供 avaje- ebeanorm 版本 6.8.1 。
对于某些额外功能,您可能有兴趣阅读this question。