Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ebean using OR in query

Tags:

ebean

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!

like image 909
Runar Halse Avatar asked Aug 30 '12 05:08

Runar Halse


1 Answers

Note that you can also use fluid style via disjunction(), conjunction() and endJunction().

For example:

Query<Conversation> query = Ebean.find(Conversation.class)
  .where().eq("group.id", groupId)
  .disjunction()
    .conjunction()
      .eq("open", false).eq("participants.user.id", userId)
    .endJunction()
    .eq("open", true)
  .endJunction()
  .orderBy("whenCreated desc");

And an example using type safe query beans it is similar but instead uses or(), and(), endAnd(), endOr() ... rather than disjunction()/conjunction() etc.

List<Customer> customers
  = new QCustomer()
  .billingAddress.city.equalTo("Auckland")
  .version.between(1,100)
  .billingAddress.country.equalTo(nz)
  .registered.before(new Date())
  .or()
    .id.greaterThan(1)
    .and()
      .name.icontains("Jim")
      .inactive.isTrue()
    .endAnd()
  .endOr()
  .orderBy()
    .name.asc()
    .id.desc()
  .findList();
like image 193
Rob Bygrave Avatar answered Sep 19 '22 08:09

Rob Bygrave