Apache Cayenne Get Collection Expression

162 views Asked by At

Using Apache Cayenne I am trying to figure out how to avoid doing iterative calls to the DB when I have a Collection of attribute values.

Say we have a persistent object Person with an attribute name of type String. If I have a List containing names of the Person I would like to generate an expression that allows a single DB call rather than iterating over the list and getting each Person in turn.

This doesn't actually exist but I would like something like this:

List<String> names = ...;
ExpressionFactory.orLikeExp(Person.NAME_PROPERTY, names);
1

There are 1 answers

0
andrus_a On BEST ANSWER

You can use ExpressionFactory.join(..):

List<Expression> pairs = new ArrayList<>(names.size());
for(String name : names) {
    // use an expression appropriate for comparison... 
    // "like" in this example, but can be "equals", etc.
    pairs.add(ExpressionFactory.likeExp(Person.NAME_PROPERTY, name));
}

Expression e = ExpressionFactory.joinExp(Expression.OR, pairs);