Spring Data Jpa tries to abstract or hide as much boilerplate code as possible. it provides the ability to create SQL from method names, but internally Spring Data Jpa provides implementations that resolve parameters from the method name and create underlying database SQL query from it.
Spring Data Jpa have 3 strategies to resolve the query from the method name defined in QueryLookupStrategy
the enum. these parameters can be set using namespace query-lookup-strategy
attribute in XML configuration and for Java configuration, EnableJpaRepositories
or EnableMongoRepositories
annotation provides queryLookupStrategy
attributes.
-
QueryLookupStrategy#CREATE
it tries to construct the underlying database-specific query from method name. Usually, it's done by removing the well-known prefix from the method name and parse the method resolve parameters. it will ignore any query defined for the method. -
QueryLookupStrategy#USE_DECLARED_QUERY
it doesnt try to parse and resolve the method name and rely on the fact that a query must alreayd been defined some where and when unable to find the will throw an exception. it will not try to construct the underlying database-specific query from method name. -
QueryLookupStrategy#CREATE_IF_NOT_FOUND
it is the default QueryLookupStrategy of Spring Data Jpa. it combines theCREATE
andUSE_DECLARED_QUERY
. It first tries to find declared query, if found then will use it else will parse the method name, resove the parameters and create a underlying database query from it.