ebean

Ebean using OR in query


I'm trying to make a query where I want to check if either the email or name of a user starts with a given string. In a sql query I would write this using

name like 'queryString%' or email like 'queryString%'

In ebean query I would expect to write something like:

find.where().or(like('name', 'queryString%'), like('email', 'queryString%'));

The problem is that the or takes in an expression, not an expressionlist, which is what I get when writing

find.where().like(...,...)

As I understand it doing a query like this:

find.where().like(.., ...).like(..., ...)

is using AND.

How can I write such a query using ebean?

Thanks!


Solution

  • Your second attempt is almost OK, you have to use just com.avaje.ebean.Expr.like() inside the or()

    find.where().or(
            com.avaje.ebean.Expr.like("email", email + "%"),
            com.avaje.ebean.Expr.like("name",  name + "%")
    ).findUnique(); 
    

    of course you can use an import for shorter code::

    import com.avaje.ebean.Expr;
    
    ...
    find.where().or(Expr.like("email", email + "%"),Expr.like("name", name + "%")).findUnique();
    

    Check the API in Javadoc: http://www.avaje.org/static/javadoc/pub/com/avaje/ebean/Expr.html