sonic boom
sonic boom

Reputation: 904

Pass native query as String to @Query

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.

Upvotes: 0

Views: 999

Answers (1)

tamHin
tamHin

Reputation: 21

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.

Upvotes: 2

Related Questions