How to refer long query, written in external file, in Spring data jpa @Query

Viewed 7655
  • I want to write query (properties or yaml) in external file to load database. -
  • This is long query and does not look good to eye when placed inside @Query("long query") in XXXRepository class.
  • Is there way to write this query in an external file (properties, yaml, xml or json) and call that file in @Query() in spring data jpa?
3 Answers

As suggested by Ralf Stuckert use META-INF/jpa-named-queries.properties to store named queries

You can use \ for split the large query into multiple lines.

Example:

Customer.findNameNative=\
SELECT C.NAME \
FROM CUST_TABLE C \
WHERE CONDITIONS

I think you're finding a place to put your big query string as readable? Like Mybatis xml. As my knowledge There is no method to do that in data jpa.

But you can put that big query inside of a Stored Procedure and call it easily like below.

Call your_stored_procedure_name(param1,param2):
Related