Spring data jpa. Find max if no result return default value

后端 未结 1 1470
无人共我
无人共我 2021-02-07 04:14

I\'ve implemented in my spring repository interface:

@Query(\"SELECT max(ch.id) FROM MyEntity ch\")
Long getMaxId();

It works correctly if db i

相关标签:
1条回答
  • 2021-02-07 04:42

    You can use coalesce like :

    @Query("SELECT coalesce(max(ch.id), 0) FROM MyEntity ch")
    Long getMaxId();
    

    If there are no data it will return 0 instead of null.

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