|
15 | 15 | */
|
16 | 16 | package org.springframework.data.jpa.repository.support;
|
17 | 17 |
|
| 18 | +import static org.springframework.data.jpa.repository.query.QueryUtils.*; |
| 19 | + |
| 20 | +import java.util.*; |
| 21 | +import java.util.function.Function; |
| 22 | + |
| 23 | +import javax.persistence.*; |
| 24 | +import javax.persistence.criteria.*; |
| 25 | + |
18 | 26 | import org.springframework.dao.EmptyResultDataAccessException;
|
19 |
| -import org.springframework.data.domain.Example; |
20 |
| -import org.springframework.data.domain.Page; |
21 |
| -import org.springframework.data.domain.PageImpl; |
22 |
| -import org.springframework.data.domain.Pageable; |
23 |
| -import org.springframework.data.domain.Sort; |
| 27 | +import org.springframework.data.domain.*; |
24 | 28 | import org.springframework.data.jpa.convert.QueryByExamplePredicateBuilder;
|
25 | 29 | import org.springframework.data.jpa.domain.Specification;
|
26 | 30 | import org.springframework.data.jpa.provider.PersistenceProvider;
|
|
37 | 41 | import org.springframework.transaction.annotation.Transactional;
|
38 | 42 | import org.springframework.util.Assert;
|
39 | 43 |
|
40 |
| -import javax.persistence.EntityManager; |
41 |
| -import javax.persistence.LockModeType; |
42 |
| -import javax.persistence.NoResultException; |
43 |
| -import javax.persistence.Parameter; |
44 |
| -import javax.persistence.Query; |
45 |
| -import javax.persistence.TypedQuery; |
46 |
| -import javax.persistence.criteria.CriteriaBuilder; |
47 |
| -import javax.persistence.criteria.CriteriaQuery; |
48 |
| -import javax.persistence.criteria.ParameterExpression; |
49 |
| -import javax.persistence.criteria.Path; |
50 |
| -import javax.persistence.criteria.Predicate; |
51 |
| -import javax.persistence.criteria.Root; |
52 |
| -import java.util.ArrayList; |
53 |
| -import java.util.Collection; |
54 |
| -import java.util.Collections; |
55 |
| -import java.util.HashMap; |
56 |
| -import java.util.List; |
57 |
| -import java.util.Map; |
58 |
| -import java.util.Optional; |
59 |
| -import java.util.function.Function; |
60 |
| - |
61 |
| -import static org.springframework.data.jpa.repository.query.QueryUtils.*; |
62 |
| - |
63 | 44 | /**
|
64 | 45 | * Default implementation of the {@link org.springframework.data.repository.CrudRepository} interface. This will offer
|
65 | 46 | * you a more sophisticated interface than the plain {@link EntityManager} .
|
|
80 | 61 | * @author Greg Turnquist
|
81 | 62 | * @author Yanming Zhou
|
82 | 63 | * @author Ernst-Jan van der Laan
|
| 64 | + * @author Diego Krupitza |
83 | 65 | */
|
84 | 66 | @Repository
|
85 | 67 | @Transactional(readOnly = true)
|
@@ -555,6 +537,20 @@ public <S extends T> boolean exists(Example<S> example) {
|
555 | 537 | return query.setMaxResults(1).getResultList().size() == 1;
|
556 | 538 | }
|
557 | 539 |
|
| 540 | + /* |
| 541 | + * (non-Javadoc) |
| 542 | + * @see org.springframework.data.jpa.repository.JpaSpecificationExecutor#exists(org.springframework.data.jpa.domain.Specification) |
| 543 | + */ |
| 544 | + @Override |
| 545 | + public boolean exists(Specification<T> spec) { |
| 546 | + |
| 547 | + CriteriaQuery<Integer> cq = this.em.getCriteriaBuilder().createQuery(Integer.class); |
| 548 | + cq.select(this.em.getCriteriaBuilder().literal(1)); |
| 549 | + applySpecificationToCriteria(spec, getDomainClass(), cq); |
| 550 | + TypedQuery<Integer> query = applyRepositoryMethodMetadata(this.em.createQuery(cq)); |
| 551 | + return query.setMaxResults(1).getResultList().size() == 1; |
| 552 | + } |
| 553 | + |
558 | 554 | /*
|
559 | 555 | * (non-Javadoc)
|
560 | 556 | * @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example)
|
|
0 commit comments