File tree Expand file tree Collapse file tree 2 files changed +17
-1
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 +17
-1
lines changed Original file line number Diff line number Diff line change 80
80
* @author Sander Krabbenborg
81
81
* @author Jesse Wouters
82
82
* @author Greg Turnquist
83
+ * @author Yanming Zhou
83
84
*/
84
85
@ Repository
85
86
@ Transactional (readOnly = true )
@@ -530,12 +531,13 @@ public <S extends T> long count(Example<S> example) {
530
531
*/
531
532
@ Override
532
533
public <S extends T > boolean exists (Example <S > example ) {
534
+
533
535
Specification <S > spec = new ExampleSpecification <>(example , this .escapeCharacter );
534
536
CriteriaQuery <Integer > cq = this .em .getCriteriaBuilder ().createQuery (Integer .class );
535
537
cq .select (this .em .getCriteriaBuilder ().literal (1 ));
536
538
applySpecificationToCriteria (spec , example .getProbeType (), cq );
537
539
TypedQuery <Integer > query = applyRepositoryMethodMetadata (this .em .createQuery (cq ));
538
- return query .setMaxResults (1 ).getSingleResult () != null ;
540
+ return query .setMaxResults (1 ).getResultList (). size () == 1 ;
539
541
}
540
542
541
543
/*
Original file line number Diff line number Diff line change @@ -2318,6 +2318,20 @@ void existsByExampleWithExcludedAttributes() {
2318
2318
assertThat (exists ).isEqualTo (true );
2319
2319
}
2320
2320
2321
+ @ Test // GH-2368
2322
+ void existsByExampleNegative () {
2323
+
2324
+ flushTestUsers ();
2325
+
2326
+ User prototype = new User ();
2327
+ prototype .setAge (4711 ); // there is none with that age
2328
+
2329
+ Example <User > example = Example .of (prototype , matching ().withIgnorePaths ("createdAt" ));
2330
+ boolean exists = repository .exists (example );
2331
+
2332
+ assertThat (exists ).isEqualTo (false );
2333
+ }
2334
+
2321
2335
@ Test // DATAJPA-905
2322
2336
void executesPagedSpecificationSettingAnOrder () {
2323
2337
You can’t perform that action at this time.
0 commit comments