Spring Data JPA 和 Exists 查询

我正在使用 Spring Data JPA (使用 Hibernate 作为 JPA 提供者) ,并希望定义一个带有 HQL 查询的 exists方法:

public interface MyEntityRepository extends CrudRepository<MyEntity, String> {


@Query("select count(e) from MyEntity e where ...")
public boolean existsIfBlaBla(@Param("id") String id);


}

当我运行这个查询时,我得到一个 java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Boolean

HQL 查询必须看起来如何才能实现这一点?我知道我可以简单地返回一个 Long 值,然后在 count > 0中检入我的 Java 代码,但是这种变通方法应该是不必要的,对吗?

189872 次浏览

I think you can simply change the query to return boolean as

@Query("select count(e)>0 from MyEntity e where ...")

If you are checking exists based on Primary key value CrudRepository already have exists(id) method.

in my case it didn't work like following

@Query("select count(e)>0 from MyEntity e where ...")

You can return it as boolean value with following

@Query(value = "SELECT CASE  WHEN count(pl)> 0 THEN true ELSE false END FROM PostboxLabel pl ...")

Since Spring data 1.12 you can use the query by Example functionnality by extending the QueryByExampleExecutor interface (The JpaRepositoryalready extends it).
Then you can use this query (among others) :

<S extends T> boolean exists(Example<S> example);

Consider an entity MyEntity which as a property name, you want to know if an entity with that name exists, ignoring case, then the call to this method can look like this :

//The ExampleMatcher is immutable and can be static I think
ExampleMatcher NAME_MATCHER = ExampleMatcher.matching()
.withMatcher("name", GenericPropertyMatchers.ignoreCase());
Example<MyEntity> example = Example.<MyEntity>of(new MyEntity("example name"), NAME_MATCHER);
boolean exists = myEntityRepository.exists(example);

Apart from the accepted answer, I'm suggesting another alternative. Use QueryDSL, create a predicate and use the exists() method that accepts a predicate and returns Boolean.

One advantage with QueryDSL is you can use the predicate for complicated where clauses.

Spring Data JPA 1.11 now supports the exists projection in repository query derivation.

See documentation here.

In your case the following will work:

public interface MyEntityRepository extends CrudRepository<MyEntity, String> {
boolean existsByFoo(String foo);
}

You can use Case expression for returning a boolean in your select query like below.

@Query("SELECT CASE WHEN count(e) > 0 THEN true ELSE false END FROM MyEntity e where e.my_column = ?1")

You can use .exists (return boolean) in jpaRepository.

if(commercialRuleMsisdnRepo.exists(commercialRuleMsisdn.getRuleId())!=true){


jsRespon.setStatusDescription("SUCCESS ADD TO DB");
}else{
jsRespon.setStatusCode("ID already exists is database");
}

It's gotten a lot easier these days!

@Repository
public interface PageRepository extends JpaRepository<Page, UUID> {


Boolean existsByName(String name); //Checks if there are any records by name
Boolean existsBy(); // Checks if there are any records whatsoever


}

Spring data provides method for checking the existence of a row using field: example: boolean existsByEmployeeIdAndEmployeeName(String employeeId, String employeeName);