具有默认值的 Spring JPA 查询方法



接口上的 Spring JPA 查询方法是否可以具有默认值?

这是常见的:Optional<DocumentTypeEntity> findByIdAndStatus(BigInteger id, String status);

我希望状态的值为"活动"(但这不起作用(Optional<DocumentTypeEntity> findByIdAndStatus(BigInteger id, "ACTIVE");

也许有人有想法? 自定义查询是我最后的手段!谢谢!

也许你可以创建两个方法:

Optional<DocumentTypeEntity> findByIdAndStatus(BigInteger id, String status);
default Optional<DocumentTypeEntity> getActiveById(BigInteger id) {
return findByIdAndStatus(id, "ACTIVE");
}

我建议使用@Query。如果你不需要应用程序中的谓词框架(Specification,QueryDSL,...(,那就更容易了。

最新更新