Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 959
doljae Avatar asked Sep 17 '25 13:09

doljae


1 Answers

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 Parts representing defined abstract query criteria.

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

like image 63
fladdimir Avatar answered Sep 20 '25 03:09

fladdimir