ta34400
ta34400

Reputation: 21

Understanding Spring JPA native query under the hood

I want to learn more about Spring JPA and what happens under the hood when a native JPA query (using PostgreSQL) is defined and called in a Repository class. I have tried searching online but all posts are related to defining and using native queries.

I am more interested to learn about what happens when my SpringBoot application calls a method that is annotated with @Query. I am mainly looking into this to try and understand the performance of executing such a query within Java.

Can anyone point me to a resource which goes in depth on what exactly happens under the hood when using Spring JPA?

Upvotes: 2

Views: 1232

Answers (2)

Jens Schauder
Jens Schauder

Reputation: 81882

Can anyone point me to a resource which goes in depth on what exactly happens under the hood when using Spring [DATA] JPA?

The authoritative source for this kind of information is the source. Let me point you to some places of interest.

Basically the following happens:

  1. find and extract the query from the annotation.
  2. possibly create a count query from that.
  3. replace spell expression with parameters.
  4. add ordering if applicable.
  5. prepare the query with the EntityManager.
  6. register parameters.
  7. add pagination limits.
  8. execute the query.
  9. transform result.

Upvotes: 1

khoibv
khoibv

Reputation: 369

  1. Output log to view what SQL query really generated and executed

For example, in application.properties:

logging.level.org.springframework.data.jpa=debug
logging.level.org.hibernate=debug

SpringBoot log reference

  1. Check the source code of spring-data-jpa here:

https://github.com/spring-projects/spring-data-jpa/blob/master/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryExecution.java

Upvotes: 0

Related Questions