ObjectDB ObjectDB

JPA CriteriaQuery -- Iterate Expression<List<Object>>

#1

Hi, I am new to  JPA and in particular the CriteriaQuery API. I have a simple CriteriaQuery where I pattern match a simple search String against field entires in a Person entity... i.e. the searchString is always decorated with %searchString%. 

The Person object can contain many Record objects. I would like to update my query below to include a similar 'like' pattern match for each applicable field (String) of each element of the List of Records (see pseudo code). 

//using MetaModel
Expression<List<Records>> records = root.get(Person_.records);
Expression<String> param = builder.parameter(String.class);

//pseudo code(i think i need something like this here)
Predicate myPred = for all records (any record.FIELD LIKE searchString)

Path<Integer> status = root.get("status");

criteriaQuery.where(
    builder.or(
        builder.like(
            root.<String>get("name"), searchString
        ),
        builder.like(
            root.<String>get("second_name"), searchString
        )
        //pseuso code (i thin i need to check my predicate here?)
        builder.like(mypredicate)


    ),
    builder.equal(status,value)
);

Any help greatly appreciated...

edit
delete
#2

There is no direct query expression for comparing against multiple different fields.

Some of your options are:

  1. Build a complex query specifying explicitly all the relevant fields.
  2. Store the fields in a Map<String, String> instead of as singular String fields.
  3. Add a new additional String field that will include all the values.
ObjectDB Support
edit
delete

Reply

To post on this website please sign in.