I use the following code to build up a query in jpa with hibernate 4.1.9.Final as the implementation:
if (!StringUtils.isBlank(filterText)) {
String search = "%" + request.getPaging().getFilter().getFilterText().trim().toLowerCase() + "%";
String dateSearch = search;
try {
dateSearch = "%" + DateTimeFormatUtils.formatDateOnlyForDB(filterText) + "%";
} catch (IllegalArgumentException exception) {
// no-op, it's okay if filterText is not a date
}
Predicate textPredicate =
criteriaBuilder.or(
buildLikePredicate(criteriaBuilder, deposit, Deposit_.id, search),
buildLikePredicate(criteriaBuilder, deposit, Deposit_.date, dateSearch),
buildLikePredicate(criteriaBuilder, deposit, Deposit_.bankAccountNumber, search),
buildLikePredicate(criteriaBuilder, deposit, Deposit_.amount, search),
buildLikePredicate(criteriaBuilder, merchant, Merchant_.merchId, search),
buildLikePredicate(criteriaBuilder, merchant, Merchant_.customName, search));
predicates.add(textPredicate);
}
return predicates.toArray(new Predicate[predicates.size()]);
and the code for buildLikePredicate() is:
private <R, J> Predicate buildLikePredicate(
final CriteriaBuilder criteriaBuilder, final From<R, J> from,
final SingularAttribute<? super J, ?> column, final String searchText) {
return criteriaBuilder.like(criteriaBuilder.lower(from.get(column).as(String.class)), searchText);
}
Basically I'm just building up a bunch of 'likes' against different columns and any column that isn't a String type I cast to a string for comparison purposes.
I run fairly extensive integration tests against in-memory HSQLDB and this all works as expected.
However when I run the server against a real Oracle database, I get the following:
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: expecting CLOSE, found '(' near line 1, column 277 [select generatedAlias0 from entities.VDeposit as generatedAlias0 inner join generatedAlias0.merchant as generatedAlias1 where ( generatedAlias1.customerId=100000 ) and ( generatedAlias0.date>=:param0 ) and ( ( lower(cast(generatedAlias0.id as varchar2(255 char))) like :param1 ) or ( lower(cast(generatedAlias0.date as varchar2(255 char))) like :param2 ) or ( lower(generatedAlias0.bankAccountNumber) like :param3 ) or ( lower(cast(generatedAlias0.amount as varchar2(255 char))) like :param4 ) or ( lower(generatedAlias1.merchId) like :param5 ) or ( lower(generatedAlias1.customName) like :param6 ) ) order by generatedAlias0.date desc]
It seems to choke up on:
cast(generatedAlias0.id as varchar2(255 char))
as being invalid syntax (note that it's hql syntax, not yet sql)
Is anyone aware of there being an Hibernate/Oracle bug when calling javax.persistence.criteria.Expression.as()?
Any ideas how to fix this?
Thanks!
UPDATE
This SO Question references the same exact problem. A ticket was filed for this bug which looks like it was fixed in Hibernate 3.6.10 and 4.1.0. However we are using version 4.1.9.Final and still seeing this issue. Was this bug reintroduced?
UPDATE
Upgraded to the latest Hibernate 4.3.6.Final and still seeing the same bug.