1

I am working on spring boot + JPA repository methods. here is my simple query method.

@Query("SELECT g FROM Greeting g where g.text = :text")
Greeting findGreetingByText(@Param("text") String sText);

Is there any way we can configure this query values in properties file instead of hardcode like above method.

for example:

@Query("Greet.SelectQuery")
 Greeting findGreetingByText(@Param("text") String sText);

//application.properties

Greet.SelectQuery=SELECT g FROM Greeting g where g.text = :text

Thank you.

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
Venkatesh Goud
  • 616
  • 2
  • 6
  • 22
  • Sure, you probably could with SpEL, but what's the point? You would have to rebuild and retest the app anyway after a change, finding the appropriate query to change would be harder, and knowing which query is executed for a method would be harder too. – JB Nizet May 21 '17 at 19:35
  • @JB Nizet Yes correct but my application has to execute a list of select queries & sent the result that's it. no other work on those results. this select query changes frequently because of this reason I am thinking instead of changing application every time i want to use properties file. – Venkatesh Goud May 21 '17 at 19:43
  • you can but probably not recommended. if it changes frequently and storing it in properties file sounds like a code smell to me. is there a way to parameterize the where condition? – alltej May 21 '17 at 20:55
  • Use `TypedQuery` with `entityManager` like this `TypedQuery query = entityManager.createQuery(DbQueries.YOUR_QUERY, YOUR_ENTITY_CLASS.class);` – Abdullah Khan May 22 '17 at 05:46
  • Have a look at my QLRM project. This has QueryExecutors http://simasch.github.io/qlrm/ – Simon Martinelli Aug 28 '18 at 13:35
  • Possible duplicate of [How to store @Query sql in external file for CrudRepository?](https://stackoverflow.com/questions/27902242/how-to-store-query-sql-in-external-file-for-crudrepository) – Jens Schauder Sep 06 '18 at 19:16

0 Answers0