Michael Stirling
Michael Stirling

Reputation: 21

Getting a hibernate.query.semanticException can't compare expressions when adding a predicate to a criteriaBuilder

I am trying to upgrade my application to run on Java 17 (I have also upgraded from JPA 2 to 3 and Hibernate 5 to 6) and a predicate for a criteriaBuilder is now throwing

org.hibernate.query.SemanticException: Cannot compare left expression of type 'java.util.Date' with right expression of type 'java.sql.Timestamp'

I don't understand why the parameters have become java.sql.Timestamp. The exception is thrown on the predicates.add line. What can I do to prevent this exception being thrown - the createdDate field in the document class is a Date field. If any more information is required please let me know
The code is below

CriteriaBuilder builder = em.getCriteriaBuilder ();
CriteriaQuery<Object[]> criteriaQuery = builder.createQuery (Object[].class);

Root<Document> root = criteriaQuery.from (Document.class);

criteriaQuery.multiselect (root.get ("documentID"), root.get ("documentDefinitionID"), root.get ("parentRef"), root.get ("statusCode"),root.get ("parentRefTypeCode"), root.get ("failureCode"), root.get ("createdBy"), root.<Date> get ("createdDate"),root.<Date> get ("createdTime"));

List<Predicate> predicates = new ArrayList<> ();

predicates.add (builder.between (root.<Date> get ("createdDate"), builder.parameter (Date.class, "dateFrom"), builder.parameter (Date.class, "dateTo")));

Upvotes: 0

Views: 8

Answers (0)

Related Questions