我有一个模型对象,看起来像这样:
@SuppressWarnings("serial")
@Entity
@Table(name = "selections")
public class Selection extends Model {
....
@ManyToMany
private Set<Market> markets;
....
}其中Selection和Market都有id属性和static Finder<Long, *> find()方法。
我试图找到包含一个集合中的Market的所有Selection对象。
@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();找到一个单一的市场对象--但是如何从集合中找到这些对象呢?而不对集合进行迭代?
发布于 2013-12-04 17:00:01
return Selection.find().where().in("markets",markets).findList();发布于 2016-04-03 11:13:34
我一直在为同样的问题而苦苦挣扎,在阅读了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();我正在使用带有sbt-play-ebean插件版本2.0.0的Play 2.4.2,它提供avaje-ebeanorm版本6.8.1。
对于一些额外的功能,您可能会对阅读this question感兴趣。
https://stackoverflow.com/questions/20140912
复制相似问题