[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
[eclipselink-users] JpaCriteriaBuilder and Predicate
|
I figured out how to get cast to work with JpaCriteriaBuilder. However
the question i have is that i had to add a
@SuppressWarnings("unchecked") to the method because of the message i
was getting "The expression of type Expression needs unchecked
conversion to conform to Expression<String>"
I could very well be doing something wrong, or imported/using the wrong
class.
/**
* Iterate over the filters and add them to the List<Predicate>. For
the fields that we allow to be searched on
* we are going to add special casting or some specific query
settings.
*
* @param filters
* @param cb
* @param from
* @return
*/
@SuppressWarnings("unchecked")
private List<Predicate> buildFilterPredicates(Map<String, String>
filters, JpaCriteriaBuilder cb, Root<Listing> from){
List<Predicate> predicates = new ArrayList<Predicate>();
for (Iterator<String> it = filters.keySet().iterator(); it.hasNext();)
{
String filterProperty = it.next(); // table column name = field
name
String filterValue = filters.get(filterProperty);
Expression<String> literal = cb
.literal(filterValue+"%");
switch (filterProperty) {
case "cmlMlsId":
predicates.add(cb.like(cb.fromExpression(cb.toExpression(from).get(filterProperty).cast("varchar")), literal));
break;
default:
predicates.add(cb.like(
from.<String> get(filterProperty), literal));
break;
}
}
return predicates;
}
Am i doing this the correct way? Do i need to provide a complete
example?
Thanks for any help.