是否可以编写如下的JPQL查询:
select count(*) > 0 from Scenario scen where scen.name = :name
这将返回true/false布尔值,具体取决于是否存在实体填充标准?
我想以这种方式使用查询:
boolean exists = entityManager.createQuery(query,Boolean.class).setParameter("name",name).getSingleResult();
Run Code Online (Sandbox Code Playgroud)
我的例子中的查询在语法上是不正确的(解析错误),但有没有正确的方法在JPQL中进行检查,这会返回布尔值,还是只能在Java代码中进行?
当我扩展CrudRepository接口时,我的子接口中有exists(ID)方法.我可以写findBy<property>方法.
有可能以某种方式编写existBy<property>将返回的方法boolean.或者用@Query(jpa query)它来注释它将返回boolean.
我知道我可以做select count(*)并返回long,但是我必须!=0检查我的服务层.
如何在查询方法中使用Spring Data中的'exists'关键字?
我想有这样的方法:
public interface ProfileRepository extends JpaRepository<Profile, Long> {
boolean existsByAttribute(String attribute);
}
Run Code Online (Sandbox Code Playgroud)
其中Attribute是Profile的字段.