-
1. Re: Request for example how to use OR or AND for AuditCriterions
mp108 Jun 8, 2017 11:08 AM (in response to mp108)1 of 1 people found this helpfulhere we go:
In case you were wondering why, this is for dataTables search field. The text entered in the search field is searched for in every column.
AuditQuery auditQuery = reader.createQuery() ...
String properties[] = filters.split(","); // get property names; COMMA delimited
String searchWords[] = searchString.split(" "); // get search strings from searchString; SPACE delimited
AuditCriterion andCriterion = null;
for(int i = 0; i < searchWords.length; i++){
String searchWord = searchWords[i];
// build up OR-criteria
AuditCriterion orCriterion = null;
for(int j = 0; j < properties.length; j++){
String property = properties[j];
if(j == 0) // first one, nothing to OR with so just add it
orCriterion = AuditEntity.property(property).like("%"+searchWord+"%");
else
orCriterion = AuditEntity.or(orCriterion, AuditEntity.property(property).like("%"+searchWord+"%"));
}
// AND together the OR-criteria
if(i == 0) // first one, nothing to AND with so just add it
andCriterion = orCriterion;
else
andCriterion = AuditEntity.and(andCriterion, orCriterion);
}
// add AND-criteria to query
auditQuery = auditQuery.add(andCriterion);