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

后端 未结 1 1469
无人共我
无人共我 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)
提交回复
热议问题