Spring Data JPA and Exists query
Solution 1
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.
Solution 2
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);
}
Solution 3
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 ...")
Solution 4
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
}
Solution 5
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) :
<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);
Related videos on Youtube
Stefan Haberl
Freelance coder passionate about all things code. I’m a senior software craftsman enthusiastic about creating lean and elegant code. I have more than 20 years of experience as a full stack dev working within the Web and Java eco systems and am an advocate of all things that make a developer’s life easier. If I’m not busy coding, you can find me roaming the peaks of the Austrian alps or travelling the world. I’m also always more than happy to chat about photography over a glass of good red wine. As freelancer I’m always on the lookout for exciting new projects. Drop me a line and let’s start talking!
Updated on August 05, 2021Comments
-
Stefan Haberl almost 3 years
I'm using Spring Data JPA (with Hibernate as my JPA provider) and want to define an
exists
method with a HQL query attached: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
.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?-
Neil Stockton almost 9 yearsobviously you could change your JPQL query to return a boolean ... by not returning "count(e)" and instead returning a boolean expression
-
-
Stefan Haberl almost 9 yearsThanks for the pointer to
exists(id)
, but my where clause contains some complex constraints... -
Stefan Haberl over 7 yearsNot really relevant in my case since my query is in HQL
-
Stefan Haberl over 7 yearsIf you'll add a working example I'll be happy to upvote.
-
Ankit Soni over 7 years@StefanHaberl I have included one example.
-
JRA_TLL about 7 years
count(e)>e
will likely only work with certain databases (e.g. Oracle). With DB2 it does not and you will have to useselect case when count(e) > 0 then true else false end from Entity e
-
bartektartanus almost 7 yearsThat's the whole point of Spring data - you don't have to write any HQL :)
-
Stefan Haberl almost 7 years@bartektartanus I disagree. Automatic query creation works for simple use cases only. If you have complex business logic and want to push that into your query (rather than filter in Java in-memory), there's no way around a nice JPA/HQL query.
-
bartektartanus almost 7 yearsYup, but if you want to check if there is a record with value on one field - this is definitely simple use case :) But for more complex, I agree - spring data is sometimes not efficient.
-
Jagger almost 7 yearsDoesn't work.
org.springframework.data.mapping.PropertyReferenceException: No property existsByXxx found for type Xxx!
-
Ankit Soni almost 7 years@Jagger, could you please provide a sample code, and also could you check if spring-data-jpa's version is 1.11 or higher?
-
Jagger almost 7 years@AnkitSoni The JPA version must be all right, because it is coming from Spring Boot version 1.4.6. I have already reopened the issue in Spring Jira. There you can see also the sample code.
-
Jagger almost 7 years@AnkitSoni It turned out, the Spring Data JPA 1.11 is first available in Spring Boot 1.5.
-
A.W. over 5 yearsThis is great and easy. Works in Spring Boot 2.0.4
boolean existsByUsername(String username);
-
JMDenver over 5 yearsdo a SQL exists query here instead of count rows. Using count has to complete all rows in an index (hopefully) or else with no index a table scan. SQL exists will return after the first encountered row rather than find every row and count them the way count(*) does. In a 10 row table this is no issue in 10s/100s of thousands and beyond this matters.
-
Rhushikesh Chaudhari over 4 yearsInitially it was not working for one of the property named tank_no. I was trying to use the method boolean existsByTank_no(String tank_no); Reason was an underscore is not supported as it is reserved character. When I changed property name to tankNo and method name to existsByTankNo(String tankNo); it is working now.
-
skwisgaar over 4 yearswhich ones? afaik it will always return boolean, no?
-
Sahil Chhabra over 4 yearsWhich all databases did you try?
-
Minkesh Jain over 4 yearsThanks, this seems like the correct approach. Works perfectly.
-
Haroldo_OK over 3 yearsBasically, checking counting something is slower than simply checking for it's existance; in the absence of better solutions, the answer works, but it is suboptimal.
-
fdaugan over 3 yearsDoes not work as expected. Returns
null
value instead offalse
-
Dave B almost 2 yearsI don't know if anybody pointed to this, for all things Hibernate I look at Vlad Mihalcea's work. I just solved this same problem with this link. vladmihalcea.com/spring-data-exists-query In my cases, I was able to leverage JPARepository.existsById().