When is the implementation for repositories generated by Spring Data? At compile time or runtime? Can I see the implementation repository implementation generated by Spring Data?
In the repository interfaces, we can add the methods like findByCustomerNameAndPhone() (assuming customerName and phone are fields in the domain object). Then, Spring provides the implementation by implementing the above repository interface methods at runtime (during the application run).
Conclusion. JPA Repository is mainly used for managing the data in a Spring Boot Application. We all know that Spring is considered to be a very famous framework of Java. We mainly use this Spring Boot to create the Spring-based stand-alone and production-based applications with a very minimal amount of effort.
MongoRepository is an interface provided by Spring Data in the package org. springframework. data. mongodb.
MongoDB and Spring Boot interact using the MongoTemplate class and MongoRepository interface.
No, for a very simple reason: there's no code generation going on. The implementation is based on proxies and a method interceptor delegating the call executions to the right places.
Effectively, a method execution can be backed by 3 types of code:
The store specific implementation of CrudRepository
. Have a look for types named Simple(Jpa|Mongo|Neo4|…)Repository
(see the JPA specific one here). They have "real" implementations for all of the methods in CrudRepository
and PagingAndSortingRepository
.
Query methods are effectively executed by QueryExecutorMethodInterceptor.doInvoke(…)
(see here). It's basically a 3-step-process to find the delegation target and invoke it. The actual execution is done in classes named (Jpa|Mongo|Neo4j…)QueryExecution
(see this one for example).
Custom implementation code is called directly, also from QueryExecutorMethodInterceptor
.
The only thing left is the query derivation, which consists of two major parts: method name parsing and query creation. For the former, have a look at PartTree
. It takes a method name and a base type and will return you a parsed AST-like structure or throw an exception if it fails to resolve properties or the like.
The latter is implemented in classes named PartTree(Jpa|Mongo|Neo4j|…)Query
and delegates to additional components for actually creating the store specific query. E.g. for JPA the interesting bits are probably in JpaQueryCreator.PredicateBuilder.build()
(see here).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With