WIP
- Either extend your interface from
CrudRepository<T, ID>
(or more specific variant of it, if the classpath contains repositories for multiple types of data sources sayJpaRepository<T, ID>
,MongoRepository<T, ID>
) - (or) copy paste selective methods from
CrudRepository/JpaRepository
interface and annotate your interface with@RepositoryDefinition(domainClass = T.class, idClass = Id.class)
. With this your repository interface would be quite clean @EnableJpaRepositories
(+ its persistence specific variants) at the@Configuration
class level- Expects the transaction manager to be named
transactionManager
& entityManagerFactory to be namedentityManagerFactory
by default. If not, we have to explicitly specify the actual names - For all default method, you can find implementation in
SimpleJpaRepository
, which is internally annotated with@Repository
(gives exception translation) &@Transactional(readOnly = true)
for transactional guarantee
findBy
,findAllBy
,findByFirstname
,findAllByFirstnameLike
e.t.c- All of the variants can be found
org.springframework.data.repository.query.parser.PartTree
. This class is responsible for parse method name & build a query tree that can be used to translate to underlying JPQL/SQL query org.springframework.data.repository.query.parser.PartTree.Subject
has additional variations that can be specified- Navigation
JpaRepositoriesAutoConfiguration
->JpaRepositoryFactoryBean
->JpaRepositoryFactory
(#createRepositoryFactory
) ->JpaQueryLookupStrategy
(#getQueryLookupStrategy
) ->CreateQueryLookupStrategy
(#create
) ->PartTreeJpaQuery
(#resolveQuery
) ->PartTree
- TODO: explciit count query
- Annotate method with
@Query
- For parameters, use index based binding
?1
(param index starts with1
) - Or based on name by using
@Param("paramName")
- or using SPEL
?#{[0]}
- spel expressions params are0
index based
- For parameters, use index based binding
- Like queries - Place
%
symbol before or after or in both places dependening on the need. For performance reasons only use prefix based searches, else database does full table scanfrom Entity e where e.param like :paramName%
from Entity e where e.param like ?1%
- SPEL support
?#{...}
specify any expression spring supports inside the braces- Default objects are supplied using
EvaluationContextExtension
. For e.g, Refer toSecurityEvaluationContextExtension
that setsSecurityExpressionRoot
as one of the root object which providesauthentication
,principal
and other methods likehasRole(..)
, e.t.c. This requires spring-security-data artifact in addition to spring data & spring security - You can do something like
@Query("select u from User u where u.emailAddress = ?#{principal.emailAddress}")
- Query params can be accessed using
#paramName
format @Query("select u from User u where u.firstname = :#{#firstname}")
to access method parameter with namefirstname
. This does not require method parameter to be annotated with@Param
, as this is pure SPEL magic & spring data has nothing to do with it
- Inside the query we can either write ``
For examples, look at https://github.com/spring-projects/spring-data-jpa/blob/master/src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java