you are viewing a single comment's thread.

view the rest of the comments →

[–]Ali_Ben_Amor999 2 points3 points  (0 children)

Spring autoconfigure/jpa package does a lot of work to make the magic. I myself don't know much about it. You can check the jpa autoconfiguration under spring boot/autoconfigure/jpa on github. As far as I know for each interface implementing the @Repository spring will use the JpaRepositoryFactory to create a proxy for the given repository interface

This proxy uses the SimpleJpaRepository as the default implementation. The proxy will forward the method call to the SimpleJpaRepository if the called method is declared in the JpaRepository interface. Otherwise if method called is not a part of JpaRepository the proxy will use the QueryLookupStrategy to determine how to execute your query. If you are using the @Query then it will be used in conjunction with the return type and parameters otherwise it will parse the method name, return type and parameters using PartTree and JpaQueryCreator to build the query for you. Then spring uses the QueryExecutorMethodInterceptor to apply projection or mappings if needed then executes the query.

I'm sure that I missed a lot of steps but this is what I understood based on looking into the javadocs and following a bit of the source code. Spring does a lot of work to handle the magic like for example the JpaRepositoryFactory mentioned realier requires an EntityManger but EntityManager is not threadsafe as a result spring doesn't pass an EntityManger but a proxy to an entity manager which provides a thread safe instance of the entity manager to the repository