Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store @Query sql in external file for CrudRepository?

I'm using spring with CrudRepositorys for database connection.

Now I require a quite long (several lines) sql query that I'd prefer to maintain in a file in classpath, rather than directly inside the code.

But how could I achieve this? My repo looks as follows:

@Query(value = "<my very long sql query>", nativeQuery = true) //how to inject file content?
@Modifying
@Transactional
public void executeSpecificSql();
like image 208
membersound Avatar asked Jan 12 '15 12:01

membersound


1 Answers

Use below steps.

  1. Create jpa-named-queries.property file in src/main/resources-->META-INF Folder enter image description here

  2. Defile your query in given properties file. enter image description here Above screenshot look closely.Here Group is Entity name, while Method should match with method define in Repository interface. Query should have object name instead table name and instead of column name provide variable name given in entity for respective field.

  3. Interface method method with property name

    List item

like image 122
Swarit Agarwal Avatar answered Sep 25 '22 13:09

Swarit Agarwal