Java – spring data JPA If no result returns the default value, find max
•
Java
I implemented in my spring repository interface:
@Query("SELECT max(ch.id) FROM MyEntity ch") Long getMaxId();
If DB is not empty, it can work normally If I start my environment with a test configuration (using h2db) – there is no data at first The result returned by getmaxid() is null I want to be here 0
Can I modify my * jparepository to get 0 results? If yes, how should I modify it?
Solution
You can use coalesce:
@Query("SELECT coalesce(max(ch.id),0) FROM MyEntity ch") Long getMaxId();
If there is no data, it will return 0 instead of null
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码