I have a model object that looks like this:
@SuppressWarnings("serial")
@Entity
@Table(name = "selections")
public class Selection extends Model {
....
@ManyToMany
private Set<Market> markets;
....
}
Where Selection and Market both have id
properties and static Finder<Long, *> find()
methods.
And i'm trying to find all the Selection
objects which contain a Market
that's within a Set.
@Override @Transactional(readOnly = true) public List<Selection> findSelections(Set<Market> markets) {
// Query?
return Selection.find().where()...findList();
}
I know i can do something like:
return Selection.find().where().eq("markets.id", market.id).findList();
to find a single market object - but what about finding those objects from the Set? Without iterating over the Set?
return Selection.find().where().in("markets",markets).findList();
I have been struggling with the same problem, and after reading another SO question and the Ebean's Interface Query documentation I came out with this:
// 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();
I am using Play 2.4.2 with sbt-play-ebean plugin version 2.0.0 , wich provides avaje-ebeanorm version 6.8.1 .
For some extra functionality you might be interested in reading this question .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.