0

I've a query which needs to be constructed as String first.

StringBuilder query = new StringBuilder();
query.append(....);
query.append(....);

I want to pass this query as a native query, like:

myTableRepository.insertData(query.toString());

So in MyTableRepository I can use it like we generally use native queries using @Query annotation.

Is it possible?

P.S. - I'm not using createNative(), due to some legacy issue, I don't want to use EntityManager.

3
  • Well it's certainly possible (via a repository extension method), but sort of defeats the whole purpose of abstracting the data access away using a repository Commented Oct 6, 2020 at 20:21
  • @crizzis, can you please redirect me to any example. I know its not ideal but I still want to know how its done. Thanks. Commented Oct 7, 2020 at 16:45
  • See here, for example Commented Oct 7, 2020 at 17:44

1 Answer 1

2

I don't think it is possible , it will exposes your code to jpql injection attack, and from perspective of DB optimizer it cause bad performance cause it have to create query each time. You can use Criteria query to build safe dynamic query.

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

Comments

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.