Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn off Query creation from method names in Spring JPA?

In Spring Data is it possible to 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 want spring security to produce an error saying that generating queries from method names has been turned off please use the explicitly @Query annotation like so.

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 turn off the the automatic query generation because I think it is easier to read the query and know what is going on rather than reading the method name and translating to what is the query that Spring data will generate, also on a large team with lots of developers some who might not yet be familiar with spring data @Query is a lot more readable?

How to turn off Query creation from method names in Spring JPA?

like image 976
ams Avatar asked Jan 14 '14 01:01

ams


2 Answers

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

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

See the 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);
}
like image 185
Kevin Bowersox Avatar answered Sep 20 '22 05:09

Kevin Bowersox


How Spring handles this is called the Query Lookup Strategy. Queries can be resolved by method names (CREATE), by manual queries (USE_DECLARED_QUERY), or both (CREATE_IF_NOT_FOUND) which defaults to method names if no manual query is found. USE_DECLARED_QUERY would give you the desired functionality, warning you if no manual query is specified.

As Kevin answered, this can be configured in xml. But as a more modern option, you can specify the lookup strategy when configuring your repository in a Java Config class with queryLookupStrategy parameter in the @Enable{store}Repositories annotation.

For example, to force manual queries, you could use the following:

@EnableJpaRepositories(queryLookupStrategy=QueryLookupStrategy.Key.USE_DECLARED_QUERY)
public class MyDatabaseConfig {
    ...
}

More info here

  • https://docs.spring.io/spring-data/jpa/docs/2.0.7.RELEASE/reference/html/#repositories.query-methods.query-lookup-strategies
  • https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/query/QueryLookupStrategy.Key.html?is-external=true
like image 31
SharpLizard Avatar answered Sep 22 '22 05:09

SharpLizard