Reputation: 1136
I have a simple CriteriaQuery
where I pattern match a simple search String
against field entires in a Person
entity... e.g the searchString
is always decorated with %searchString%
//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)
);
The Person
object can contain many Record
objects. I would like to update my query above to include a similar 'like' pattern match for each applicable field (String
) of each element of the List of Record
s. I obtain the Record
s like below:
//using MetaModel
Expression<List<Records>> records = root.get(Person_.records);
Expression<String> param = builder.parameter(String.class);
I have updated my code with pseudocode of what i 'think' i want.Any help much appreciated.
Upvotes: 1
Views: 6131
Reputation: 2993
At least for jpa 2.1 it is possible. First declare a join between the 2 entities and then use the join created to get through the variable.
//using MetaModel
Expression<List<Records>> records = root.get(Person_.records);
Expression<String> param = builder.parameter(String.class);
SetJoin<Person, Record> joinRecord = root.joinSet("records", JoinType.INNER);
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
),
builder.like(
joinRecord.<String>get("FIELD_NAME"), searchString
)
),
builder.equal(status,value)
);
Hope it helps
Upvotes: 3
Reputation: 1136
As far as I can tell this is not possible using CriteriaQuery. I need to use a SQL query... Here's how I did it...
https://stackoverflow.com/a/24488081/1843591
Upvotes: 0