5

I need to run the following query in QueryDsl Native MySQL.

SELECT cast(json_unquote(JSON_EXTRACT(vendor_master,'$.vendor.currency')) as char) as currency FROM vendor;

The above query has 3 mysql functions which are nested such as cast,json_unquote and json_extract. In QueryDsl, I have created separate Expression for each function as shown in the below code.

    SimpleExpression<String> exp1=Expressions.simpleTemplate(String.class,"json_extract({0},{1})",vendor.vendorMaster,"$.vendor.currency");
SimpleExpression<String> exp2=Expressions.simpleTemplate(String.class,"json_unquote({0})",exp2);
SimpleExpression<String> exp3=Expressions.simpleTemplate(String.class,"cast({0} as char) as currency",exp3);
List<Tuple> tuples=sqlQueryFactory.select(exp3).from(vendor).fetch();

I have to create more number of expressions as much as no of functions. Is it possible to call more than one function in single expression in order to reduce complexity?

1 Answer 1

6

I found the answer for the question.

SimpleExpression<String> exp=Expressions.simpleTemplate(String.class,"cast(json_unquote(json_extract({0},{1})) as char) as currency",vendor.vendorMaster,"$.vendor.currency"); 
List<Tuple> tuples=sqlQueryFactory.select(exp).from(vendor).fetch();

Thanks for your response.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Solved my problem too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.