Search code examples
springspring-dataspring-data-jpa

What is the difference between query-methods find…By, read…By, query…By, and get…By in spring data?


I was looking at docs of spring data, and didn't find a reasons to use methods read...By, get...By instead of find...By (as it usually done). Please clarify:

  • what does this methods do?
  • or what is purpose of this methods is?
  • In what cases better use this methods?
  • what is the difference between them?

Could you write an example of query..By method?


Solution

  • EDIT:
    This answer covers Spring query creation mechanism described in docs
    If you are looking for the differences between concrete methods findById(..) vs getById(..) you can find nice desciption here (thanks to @smile comment)

    ORIGINAL ANSWER:
    I don't know how about other subprojects, but for Spring Data JPA (1.10.2) these methods will work as aliases. Each method invocation will generate identical criteria query (and identical SQL query).

    Internally there is no distinction between these prefixes. It's used only for query pattern matching:

    private static final String QUERY_PATTERN = "find|read|get|query|stream";
    

    https://github.com/spring-projects/spring-data-commons/blob/8bc022ebd7097b921ae1ef6c87f0ae9fc05bba5f/src/main/java/org/springframework/data/repository/query/parser/PartTree.java#L54

    The same approach is used for remove...By vs delete...By methods:

    private static final String DELETE_PATTERN = "delete|remove";