• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JPA criteria API function convert or cast

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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'
 
And then the flying monkeys attacked. My only defense was this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic