Spring CRUD repository: is there findOneByMaxXYZColumn()?

前端 未结 3 839
小鲜肉
小鲜肉 2020-12-16 09:54

My requirement:

fetch ONE object (e.g RetainInfo ) from table RETAIN_INFO if VERSION column has max value

Does CRUD repository support for an interface met

相关标签:
3条回答
  • 2020-12-16 10:32

    Spring Data doesn't provide an expression to select a max value. All supported query parts could be found in the Spring 1.2.0.RELEASE docs: Appendix A. Namespace reference or line 182 of org.springframework.data.repository.query.parser.Part.

    Also feel free to create a feature request at Spring's Jira page.

    0 讨论(0)
  • 2020-12-16 10:36

    You can also use findFirst to get the first result. Before getting the result, make sure to use Orderby and then the ascending(Asc) or descending(Desc). As an example if you want to order by version and retrieve based on productName

    RetainInfo findFirstByProductNameOrderByVersionDesc(String productName);
    
    0 讨论(0)
  • 2020-12-16 10:44

    You could use limiting in combination with sorting (spring data reference:limit query results). Declare a method similar to the following in your CrudRepository interface :

        RetainInfo findTopByCountryOrderByRetVersionDesc(String country);
    
    0 讨论(0)
提交回复
热议问题