• 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
Sheriff
Posts: 36026
430
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'
 
I can't take it! You are too smart for me! Here is the tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!