How to refer long query, written in external file, in Spring data jpa @Query

后端 未结 1 692
离开以前
离开以前 2021-02-09 03:40
  • I want to write query (properties or yaml) in external file to load database. -
  • This is long query and does not look good to eye when placed inside @Query(\"long
1条回答
  •  再見小時候
    2021-02-09 04:01

    You can use named queries, where the queries have to be defined in a file called META-INF/jpa-named-queries.properties. See the spring example:

    User.findBySpringDataNamedQuery=select u from User u where u.lastname=?1
    

    Reference the query by name in the annotation in your repository, here the corresponding repository example from spring:

    @Query(name = "User.findBySpringDataNamedQuery", countProjection = "u.firstname")
    Page findByNamedQueryAndCountProjection(String firstname, Pageable page);
    

    0 讨论(0)
提交回复
热议问题