Java Spring Data JPA 和 Exists 查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30392129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Data JPA and Exists query
提问by Stefan Haberl
I'm using Spring Data JPA (with Hibernate as my JPA provider) and want to define an exists
method with a HQL query attached:
我正在使用 Spring Data JPA(使用 Hibernate 作为我的 JPA 提供程序)并希望定义一个exists
带有 HQL 查询的方法:
public interface MyEntityRepository extends CrudRepository<MyEntity, String> {
@Query("select count(e) from MyEntity e where ...")
public boolean existsIfBlaBla(@Param("id") String id);
}
When I run this query, I get a java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Boolean
.
当我运行这个查询时,我得到一个java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Boolean
.
How does the HQL query have to look like to make this work? I know I could simply return a Long value and afterwards check in my Java code if count > 0
, but that workaround shouldn't be necessary, right?
HQL 查询必须如何才能使其工作?我知道我可以简单地返回一个 Long 值,然后检查我的 Java 代码 if count > 0
,但是这种解决方法应该不是必要的,对吧?
采纳答案by K. Siva Prasad Reddy
I think you can simply change the query to return boolean as
我认为您可以简单地更改查询以返回布尔值
@Query("select count(e)>0 from MyEntity e where ...")
PS:
If you are checking exists based on Primary key value CrudRepository
already have exists(id)
method.
PS:如果您根据主键值检查是否存在,则CrudRepository
已有exists(id)
方法。
回答by Runomu
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 ...")
回答by Stephane L
Since Spring data 1.12 you can use the query by Example functionnality by extending the QueryByExampleExecutor
interface (The JpaRepository
already extends it).
Then you can use this query (among others) :
从 Spring data 1.12 开始,您可以通过扩展QueryByExampleExecutor
接口(JpaRepository
已经扩展它)来使用示例功能的查询。
然后你可以使用这个查询(等等):
<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 :
考虑一个MyEntity
作为属性的实体name
,您想知道是否存在具有该名称的实体,忽略大小写,则对此方法的调用可能如下所示:
//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);
回答by Narasimha
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.
除了接受的答案之外,我还建议另一种选择。使用QueryDSL,创建谓词并使用exists()
接受谓词并返回布尔值的方法。
One advantage with QueryDSL is you can use the predicate for complicated where clauses.
QueryDSL 的一个优点是您可以将谓词用于复杂的 where 子句。
回答by Ankit Soni
Spring Data JPA 1.11now supports the exists
projection in repository query derivation.
Spring Data JPA 1.11现在支持exists
存储库查询派生中的投影。
See documentation here.
请参阅此处的文档。
In your case the following will work:
在您的情况下,以下内容将起作用:
public interface MyEntityRepository extends CrudRepository<MyEntity, String> {
boolean existsByFoo(String foo);
}
回答by Sahil Chhabra
You can use Case
expression for returning a boolean
in your select query like below.
您可以使用Case
表达式boolean
在您的选择查询中返回 a ,如下所示。
@Query("SELECT CASE WHEN count(e) > 0 THEN true ELSE false END FROM MyEntity e where e.my_column = ?1")
回答by Yosua Simanjuntak
You can use .exists (return boolean) in jpaRepository.
您可以在 jpaRepository 中使用 .exists(返回布尔值)。
if(commercialRuleMsisdnRepo.exists(commercialRuleMsisdn.getRuleId())!=true){
jsRespon.setStatusDescription("SUCCESS ADD TO DB");
}else{
jsRespon.setStatusCode("ID already exists is database");
}
回答by Dogan Eren
You can just return a Boolean like this:
你可以像这样返回一个布尔值:
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;
import org.springframework.data.repository.query.Param;
@QueryHints(@QueryHint(name = org.hibernate.jpa.QueryHints.HINT_FETCH_SIZE, value = "1"))
@Query(value = "SELECT (1=1) FROM MyEntity WHERE ...... :id ....")
Boolean existsIfBlaBla(@Param("id") String id);
Boolean.TRUE.equals(existsIfBlaBla("0815"))
could be a solution
Boolean.TRUE.equals(existsIfBlaBla("0815"))
可能是一个解决方案