Skip to content

Commit 2a05e5e

Browse files
Thomas Darimontodrotbohm
authored andcommitted
DATAJPA-665 - Add QueryDslJpaRepository.exists(…) method wich accepts a Querydsl predicate.
Introduced QueryDslJpaRepository.exists(Predicate) method to QueryDslJpaRepository. Related ticket: DATACMNS-636. Original pull request: #131.
1 parent e8436d5 commit 2a05e5e

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/main/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepository.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2012 the original author or authors.
2+
* Copyright 2008-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,6 +46,7 @@
4646
* {@link QueryDslPredicateExecutor}.
4747
*
4848
* @author Oliver Gierke
49+
* @author Thomas Darimont
4950
*/
5051
public class QueryDslJpaRepository<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements
5152
QueryDslPredicateExecutor<T> {
@@ -142,6 +143,13 @@ public long count(Predicate predicate) {
142143
return createQuery(predicate).count();
143144
}
144145

146+
/* (non-Javadoc)
147+
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#exists(com.mysema.query.types.Predicate)
148+
*/
149+
public boolean exists(Predicate predicate) {
150+
return createQuery(predicate).exists();
151+
}
152+
145153
/**
146154
* Creates a new {@link JPQLQuery} for the given {@link Predicate}.
147155
*

src/test/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepositoryTests.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2014 the original author or authors.
2+
* Copyright 2008-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -325,4 +325,15 @@ public void shouldSupportSortByOperatorWithDateExpressions() {
325325
assertThat(users, hasSize(3));
326326
assertThat(users, hasItems(dave, carter, oliver));
327327
}
328+
329+
/**
330+
* @see DATAJPA-665
331+
*/
332+
@Test
333+
public void shouldSupportExistsWithPredicate() throws Exception {
334+
335+
assertThat(repository.exists(user.firstname.eq("Dave")), is(true));
336+
assertThat(repository.exists(user.firstname.eq("Unknown")), is(false));
337+
assertThat(repository.exists((Predicate) null), is(true));
338+
}
328339
}

0 commit comments

Comments
 (0)