• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA criteria API function convert or cast

 
Sean Barry
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'm trying to convert the following SQL to CriteriaBuilder code

SQL:

SELECT * FROM logging WHERE CONVERT(log_action, CHAR(100)) = 'delete'
log_action is defined as a varbinary(32).

CODE:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Logging> query = cb.createQuery(Logging.class);
Root<Logging> from = query.from(Logging.class);
Expression<Byte> path = from.get("logAction");
Expression<String> convertFunction = cb.function("CONVERT", String.class, path);

query.select(from);

Predicate predicate = cb.equal(convertFunction, "delete");

query.where(predicate);

return em.createQuery(query).getResultList();
Any ideas what I'm doing wrong?

Thanks,

Sean
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean,
Welcome to CodeRanch!

What error do you get? What part of that doesn't work?

One way to figure this out is to simplify it. Does the Criteria API code you wrote work if you simplify to the following two queries? If not, post the code for just that along with the error. If it does, just post the error because then we know the problem is with convert.

SELECT * FROM logging
SELECT * FROM logging WHERE logAction = 'delete'
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic