|
1 | 1 | /* |
2 | | - * Copyright 2025 the original author or authors. |
| 2 | + * Copyright 2025-present the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
6 | 6 | * You may obtain a copy of the License at |
7 | 7 | * |
8 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 | * |
10 | 10 | * Unless required by applicable law or agreed to in writing, software |
11 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
16 | 16 | package example.springdata.jpa.simple; |
17 | 17 |
|
18 | 18 | import static org.assertj.core.api.Assertions.*; |
| 19 | +import static org.springframework.data.jpa.criteria.Expressions.*; |
19 | 20 |
|
20 | 21 | import jakarta.persistence.EntityManager; |
21 | 22 |
|
@@ -85,4 +86,27 @@ void updateWithSpecification() { |
85 | 86 | assertThat(result).extracting(User::getLastname).containsOnly("Brown"); |
86 | 87 | } |
87 | 88 |
|
| 89 | + @Test |
| 90 | + void updateWithTypedSpecification() { |
| 91 | + |
| 92 | + PredicateSpecification<User> isFirstName = (from, cb) -> cb.equal(path(from, User::getFirstname), "Dave"); |
| 93 | + PredicateSpecification<User> isLastName = (from, cb) -> cb.equal(path(from, User::getLastname), "Matthews"); |
| 94 | + |
| 95 | + UpdateSpecification<User> specification = UpdateSpecification.<User> update((root, update, criteriaBuilder) -> { |
| 96 | + update.set(path(root, User::getFirstname), "Dan"); |
| 97 | + update.set(path(root, User::getLastname), "Brown"); |
| 98 | + }).where(isFirstName.and(isLastName)); |
| 99 | + |
| 100 | + assertThat(repository.update(specification)).isEqualTo(1); |
| 101 | + entityManager.clear(); |
| 102 | + |
| 103 | + assertThat(repository.findAll(isFirstName)).isEmpty(); |
| 104 | + |
| 105 | + List<User> result = repository.findAll((from, cb) -> cb.equal(path(from, User::getFirstname), "Dan")); |
| 106 | + |
| 107 | + assertThat(result).hasSize(1); |
| 108 | + assertThat(result).extracting(User::getFirstname).containsOnly("Dan"); |
| 109 | + assertThat(result).extracting(User::getLastname).containsOnly("Brown"); |
| 110 | + } |
| 111 | + |
88 | 112 | } |
0 commit comments