How to disable query creation from method names in Spring JPA?

In Spring Data, can I turn off query generation from method names?

Given the interface

public interface UserRepository extends Repository<User, Long> { List<User> findByEmailAddressAndLastname(String emailAddress, String lastname); } 

I would like Spring to provide an error indicating that query generation from method names is disabled, please use the @Query annotation explicitly.

 public interface UserRepository extends Repository<User, Long> { @Query("select u from User u where u.emailAddress = ?1 and u.lastname = ?2") List<User> findByEmailAddressAndLastname(String emailAddress, String lastname); } 

I want to disable automatic query creation, because, in my opinion, it’s easier to read the request and know what is happening, rather than reading the method name and translating into what the Spring request will generate also in a large team with a large number of developers, some of which maybe not familiar with Spring @Query data, more readable?

How to disable query creation from method names in Spring JPA?

+7
java spring spring-data spring-data-jpa
source share
1 answer

You can specify query-lookup-strategy in the repositories tag in the configuration.

 <repositories query-lookup-strategy="use-declared-query"/> 

See documentation

User.java

 @Entity @NamedQuery(name="User.findByEmailAddressAndLastName", query="select u from User u where u.emailAddress = ?1 and u.lastname = ?2") public User{ } 

UserRepository.java

 public interface UserRepository extends Repository<User, Long> { List<User> findByEmailAddressAndLastname(String emailAddress, String lastname); } 
+5
source share

All Articles