简体   繁体   中英

How query method of Spring Data JPA generate query?

When a method is created under a specific rule of Spring Data JPA, a method that calls the corresponding query is created.

For example,

public interface CustomerJpaRepository implements JpaRepository<Customer, Long>{

    public List<Customer> findByName(String name);
}

findByName() generate the query similar to one below.

select * from Customer where name = name;

I am curious about this principle. To be precise, I'm curious about the code that parses this method and turns it into a query.

I looked at the code of the SimpleJpaRepository class that implements JpaRepository , but could not find a clue. (Of course, there is a possibility that I did not find it).

In summary, when a method consisting of specific words is declared in JpaRepository, I am curious about the code that actually executes this method internally . More specifically, I'd like to see the code that makes this method works.

If there is no code to do this internally (I personally doubt it's possible...), I want to know how it is implemented in detail, if there is a link or material that explains the principle or internal process, please share related references.

The parsing logic for creating queries from spring-data repository method names is currently mainly declared in the package org.springframework.data.repository.query.parser .

Basically, a repository method name string is parsed into a PartTree , which contains Part s representing defined abstract query criteria.

The PartTree can then be used to create a more specific query object, eg with a JpaQueryCreator , or a RedisQueryCreator , depending on the type of repository.

I recommend you to check this Query Creation spring doc It explains the rules of how the method convert into a query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM