0

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);
andrus_a
  • 2,528
  • 1
  • 16
  • 10
tarka
  • 5,289
  • 10
  • 51
  • 75

1 Answers1

2

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);
andrus_a
  • 2,528
  • 1
  • 16
  • 10