Q:

Ebean: Join multiple expressions with OR

I use the PLAY Framework 2.2.1 and I want to build a query to search the database for terms I get from an input form.
This is what I have got so far but it doesn't work:
...
List<String> terms;
public static Model.Finder<Integer, XYZ> find = new Model.Finder<Integer, XYZ.class>(Integer.class, XYZ.class);
ExpressionList<XYZ> el = XYZ.find.where();

for(String term : terms){
    el.add(Expr.ilike("name", term + "%"));
}

List<XYZ> results = XYZ.find.where().add(el.disjunction()).findList();

I don't know how to add and connect multiple expressions with OR to my query.

A:

I think that you'll do that fastest with custom query (like showed in select sample)
com.avaje.ebean.Query<Xyz> query = com.avaje.ebean.Ebean.createQuery(Xyz.class);
query.where("lower(first_name) like :first_name OR lower(last_name) like :last_name ")
     .setParameter("first_name", "John".toLowerCase()+"%")
     .setParameter("last_name", "Doe".toLowerCase() + "%");

List<Xyz> xyzList = query.findList();

You can also make more advanced combination with numbered params, ie:
List<String> sqlList = new ArrayList<>();
List<Object> paramsList = new ArrayList<>();

sqlList.add("lower(first_name) like ?");
paramsList.add("John".toLowerCase() + "%");

sqlList.add("lower(last_name) like ?");
paramsList.add("John".toLowerCase() + "%");

if (optionallyCheckTheNick) {
    sqlList.add("lower(nick) like ?");
    paramsList.add("johnie".toLowerCase() + "%");
}

Query<Xyz> query = Ebean.createQuery(Xyz.class);
query.where(StringUtils.join(sqlList, " OR "));

int i = 1; // first param uses index 1 NOT 0!
for (Object param : paramsList) {
    query.setParameter(i, param);
    i++;
}

List<Xyz> xyzList = query.findList()

Just make sure that you always adds to sqlList and paramsList in pairs!

