16

I am in little hurry so, i just want to ask a quick question about querydsl. According to my research, query dsl doesn't support stored procedure but can support database functions. My Question is how can we invoke those database functions using querydsl?

3
  • Wouldn't this article answer your question ?
    – Laurent S.
    Apr 10, 2014 at 9:46
  • yeah, i have gone through it lots of time. But it is little out dated based on the current version of querydsl.
    – Mani Rai
    Apr 10, 2014 at 9:51
  • My bad. Consider I haven't said anything then :-)
    – Laurent S.
    Apr 10, 2014 at 9:56

2 Answers 2

27

You can use TemplateExpression based injections of arbitrary JPQL syntax into your query.

e.g.

query.where(Expressions.booleanTemplate("func1({0}, {1})", arg1, arg2));

If you use Hibernate 4.3 or any other JPA 2.1 compliant provider you can use the FUNCTION syntax to invoke SQL functions https://bugs.eclipse.org/bugs/show_bug.cgi?id=350843

So the example would turn into

query.where(Expressions.booleanTemplate("function('func1', {0}, {1})", arg1, arg2)"));
6
  • Wow.. what a coincidence timo.. i just saw and read slide.. you got it almost all in it i need to know.. thank you.. and i ll definitely try your suggestion..
    – Mani Rai
    Apr 10, 2014 at 22:20
  • 2
    @Timo I'm trying this but i receive an: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node Sep 3, 2015 at 19:20
  • Nice one. And also is it possible to handle more functions (nested inner functions) in a single call?
    – spr
    Jan 7, 2017 at 5:44
  • so ... basically ... QueryDSL doesnt support typesafe functions although it would be possible to extract the types via @NamedStoredProcedureQuery. Thats sad.
    – specializt
    Apr 16, 2019 at 12:49
  • Hi, @renanleandrof, have you solve the problem? I got the same error: unexpected AST node. My code: Expressions.booleanTemplate("FIND_IN_SET({0}, {1})", arg1, arg2)
    – Elva
    Aug 10, 2021 at 2:26
0

example of call mysql function find_in_set:

query.where(Expressions.booleanTemplate("find_in_set({0}, {1}) > 0", 1, "1,2,3"));

'> 0' Is a must, else thorw unexpected AST node

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.