相关疑难解决方法(0)

JPQL语句返回布尔值

是否可以编写如下的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代码中进行?

select jpa jpql jpa-2.0

35
推荐指数
2
解决办法
3万
查看次数

存在Spring数据CrudRepository

当我扩展CrudRepository接口时,我的子接口中有exists(ID)方法.我可以写findBy<property>方法.

有可能以某种方式编写existBy<property>将返回的方法boolean.或者用@Query(jpa query)它来注释它将返回boolean.

我知道我可以做select count(*)并返回long,但是我必须!=0检查我的服务层.

spring spring-data spring-data-jpa

28
推荐指数
4
解决办法
4万
查看次数

如何在Spring Data中使用exists关键字来检查实体的存在?

如何在查询方法中使用Spring Data中的'exists'关键字?

我想有这样的方法:

public interface ProfileRepository extends JpaRepository<Profile, Long> {
  boolean existsByAttribute(String attribute);
}
Run Code Online (Sandbox Code Playgroud)

其中AttributeProfile的字段.

解决方法是使用自定义实现.但附录定义存在为关键字.有人可以举个例子来说明如何使用这个关键字吗?

spring-data

12
推荐指数
1
解决办法
2万
查看次数

标签 统计

spring-data ×2

jpa ×1

jpa-2.0 ×1

jpql ×1

select ×1

spring ×1

spring-data-jpa ×1