From 2a05e5e1c93380e88672fc14c2556f7945f57282 Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Fri, 23 Jan 2015 09:50:34 +0100 Subject: [PATCH] =?UTF-8?q?DATAJPA-665=20-=20Add=20QueryDslJpaRepository.e?= =?UTF-8?q?xists(=E2=80=A6)=20method=20wich=20accepts=20a=20Querydsl=20pre?= =?UTF-8?q?dicate.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Introduced QueryDslJpaRepository.exists(Predicate) method to QueryDslJpaRepository. Related ticket: DATACMNS-636. Original pull request: #131. --- .../repository/support/QueryDslJpaRepository.java | 10 +++++++++- .../support/QueryDslJpaRepositoryTests.java | 13 ++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepository.java b/src/main/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepository.java index 7fa6812de..a66b7c7ab 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepository.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2008-2012 the original author or authors. + * Copyright 2008-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,6 +46,7 @@ import com.mysema.query.types.path.PathBuilder; * {@link QueryDslPredicateExecutor}. * * @author Oliver Gierke + * @author Thomas Darimont */ public class QueryDslJpaRepository extends SimpleJpaRepository implements QueryDslPredicateExecutor { @@ -142,6 +143,13 @@ public class QueryDslJpaRepository extends SimpleJpa return createQuery(predicate).count(); } + /* (non-Javadoc) + * @see org.springframework.data.querydsl.QueryDslPredicateExecutor#exists(com.mysema.query.types.Predicate) + */ + public boolean exists(Predicate predicate) { + return createQuery(predicate).exists(); + } + /** * Creates a new {@link JPQLQuery} for the given {@link Predicate}. * diff --git a/src/test/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepositoryTests.java b/src/test/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepositoryTests.java index 00731dcc2..335440dbe 100644 --- a/src/test/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepositoryTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepositoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2008-2014 the original author or authors. + * Copyright 2008-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -325,4 +325,15 @@ public class QueryDslJpaRepositoryTests { assertThat(users, hasSize(3)); assertThat(users, hasItems(dave, carter, oliver)); } + + /** + * @see DATAJPA-665 + */ + @Test + public void shouldSupportExistsWithPredicate() throws Exception { + + assertThat(repository.exists(user.firstname.eq("Dave")), is(true)); + assertThat(repository.exists(user.firstname.eq("Unknown")), is(false)); + assertThat(repository.exists((Predicate) null), is(true)); + } }