I want to use wild card search on a field which is a Long value (in Entity class) and Integer in database.Here Contact is entity class which has field : Id as Long value in entity class and as a Integer in database.The following is code.
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Tuple> query = builder.createTupleQuery();
Root<Contact> root = query.from(Contact.class);
List<Predicate> predicates = new ArrayList<Predicate>();
if (searchCriteria.getContactId() != null) {
if(searchCriteria.getContactId().contains("%")){
predicates.add(builder.like(
(root.get(Contact_.id)),-->Note this line
builder.literal(searchCriteria.getContactId()+"%")));
}
}
Here builder.like will not accept root.get(Contact_.id) as Contact_.id is a long value.We will get compilation error here.Here searchCriteria.getContactId() is string value.
Is there any way to cast the root.get(Contact_.id) as string type?
public static volatile SingularAttribute<Contact, Long> id;
So far I have tried with
((Expression<String>)(root.get(Contact_.id).as(String.class).
But i am getting query syntax exception.It is not able to convert it to perfect syntax. It is giving query like -> ( cast(generatedAlias0.id as varchar(255)) but not as ( cast(generatedAlias0.id as string)) when i look at logs.