1

I have custom long sql queries strings in my JPA repository interface.I feel move this queries to some properties files because when i want execute queries against my DB i have to do remove all '+' plus sign and double quote's. Instead of remove that better put all queries into properties or yaml files. I can easy to use and modify.Please suggest me this way is a correct approach.

@Repository
public interface LoanRepository extends JpaRepository<Loan, Integer> {

     @(name= "{long query}")
     public List<Loan> findLoansByIdAndBalance();
}
  • What happen if you put at the beginning `$` then it would be `"${long.query}"` – Jonathan JOhx Aug 30 '19 at 20:23
  • its throwing error java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '${long.query}' at line 1 – Rajini Rengaraj Sep 01 '19 at 17:06
  • Please any one help me. is there other way to achieve this? – Rajini Rengaraj Sep 08 '19 at 06:16
  • Possibly you can add `constant variables` in a class and call it, something for example, static final LOANS_BY_ID_AND_BALANCE = "SELECT x FROM x"; and calling in @Query(Queries.LOANS_BY_ID_AND_BALANCE) – Jonathan JOhx Sep 08 '19 at 06:25

1 Answers1

0

This is an example for how to get values from yml file.

In your application.yml:

data:
   query:
      queryGetNames: here_your_query

And in your class you call that value with this:

@Setter(onMethod_ = {@Value("${data.query.queryGetNames}")})
private String query;

Note: The @Setter annotation is from lombok

  • 1
    @femando i want to use within repository interface. is it possible ? – Rajini Rengaraj Sep 01 '19 at 17:04
  • I created separate interface there i put all queries and refer from there to my JPA repository interface. Because @Query VALUE property allowing only final string values. So we can't declare final variable without values. So i think we can't get values from application properties. please correct me if i am wrong... – Rajini Rengaraj Sep 01 '19 at 17:53
  • I referred this link also its helped me lot https://stackoverflow.com/questions/27902242/how-to-store-query-sql-in-external-file-for-crudrepository – Rajini Rengaraj Sep 12 '19 at 09:37