File tree Expand file tree Collapse file tree 2 files changed +18
-3
lines changed
main/java/org/springframework/data/jpa/repository/support
test/java/org/springframework/data/jpa/repository Expand file tree Collapse file tree 2 files changed +18
-3
lines changed Original file line number Diff line number Diff line change 75
75
* @author Moritz Becker
76
76
* @author Sander Krabbenborg
77
77
* @author Jesse Wouters
78
- * @param <T> the type of the entity to handle
79
- * @param <ID> the type of the entity's identifier
78
+ * @author Greg Turnquist
79
+ * @author Yanming Zhou
80
80
*/
81
81
@ Repository
82
82
@ Transactional (readOnly = true )
@@ -527,12 +527,13 @@ public <S extends T> long count(Example<S> example) {
527
527
*/
528
528
@ Override
529
529
public <S extends T > boolean exists (Example <S > example ) {
530
+
530
531
Specification <S > spec = new ExampleSpecification <>(example , this .escapeCharacter );
531
532
CriteriaQuery <Integer > cq = this .em .getCriteriaBuilder ().createQuery (Integer .class );
532
533
cq .select (this .em .getCriteriaBuilder ().literal (1 ));
533
534
applySpecificationToCriteria (spec , example .getProbeType (), cq );
534
535
TypedQuery <Integer > query = applyRepositoryMethodMetadata (this .em .createQuery (cq ));
535
- return query .setMaxResults (1 ).getSingleResult () != null ;
536
+ return query .setMaxResults (1 ).getResultList (). size () == 1 ;
536
537
}
537
538
538
539
/*
Original file line number Diff line number Diff line change @@ -2058,6 +2058,20 @@ void existsByExampleWithExcludedAttributes() {
2058
2058
assertThat (exists ).isEqualTo (true );
2059
2059
}
2060
2060
2061
+ @ Test // GH-2368
2062
+ void existsByExampleNegative () {
2063
+
2064
+ flushTestUsers ();
2065
+
2066
+ User prototype = new User ();
2067
+ prototype .setAge (4711 ); // there is none with that age
2068
+
2069
+ Example <User > example = Example .of (prototype , matching ().withIgnorePaths ("createdAt" ));
2070
+ boolean exists = repository .exists (example );
2071
+
2072
+ assertThat (exists ).isEqualTo (false );
2073
+ }
2074
+
2061
2075
@ Test // DATAJPA-905
2062
2076
void executesPagedSpecificationSettingAnOrder () {
2063
2077
You can’t perform that action at this time.
0 commit comments