Ishtiaq Maqsood
Ishtiaq Maqsood

Reputation: 580

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

Upvotes: 13

Views: 10877

Answers (3)

Sastrija
Sastrija

Reputation: 3374

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

You can use \ to split the long query into multiple lines.

Example:

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

Upvotes: 2

Mafei
Mafei

Reputation: 3801

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):

Upvotes: 0

Ralf Stuckert
Ralf Stuckert

Reputation: 2200

You can use named queries, where the queries have to be defined in a file called META-INF/jpa-named-queries.properties. See the spring example:

User.findBySpringDataNamedQuery=select u from User u where u.lastname=?1

Reference the query by name in the annotation in your repository, here the corresponding repository example from spring:

@Query(name = "User.findBySpringDataNamedQuery", countProjection = "u.firstname")
Page<User> findByNamedQueryAndCountProjection(String firstname, Pageable page);

Upvotes: 12

Related Questions