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!
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With