18
18
import static org .assertj .core .api .Assertions .*;
19
19
20
20
import jakarta .persistence .EntityManager ;
21
+ import jakarta .persistence .OptimisticLockException ;
21
22
import jakarta .persistence .PersistenceContext ;
22
23
23
24
import java .util .Arrays ;
32
33
import org .springframework .data .jpa .domain .sample .PersistableWithIdClassPK ;
33
34
import org .springframework .data .jpa .domain .sample .SampleEntity ;
34
35
import org .springframework .data .jpa .domain .sample .SampleEntityPK ;
36
+ import org .springframework .data .jpa .domain .sample .VersionedUser ;
35
37
import org .springframework .data .jpa .repository .JpaRepository ;
36
38
import org .springframework .data .repository .CrudRepository ;
37
39
import org .springframework .test .context .ContextConfiguration ;
46
48
* @author Jens Schauder
47
49
* @author Greg Turnquist
48
50
* @author Krzysztof Krason
51
+ * @author Yanming Zhou
49
52
*/
50
53
@ ExtendWith (SpringExtension .class )
51
54
@ ContextConfiguration ({ "classpath:infrastructure.xml" })
@@ -56,11 +59,13 @@ class JpaRepositoryTests {
56
59
57
60
private JpaRepository <SampleEntity , SampleEntityPK > repository ;
58
61
private CrudRepository <PersistableWithIdClass , PersistableWithIdClassPK > idClassRepository ;
62
+ private JpaRepository <VersionedUser , Long > versionedUserRepository ;
59
63
60
64
@ BeforeEach
61
65
void setUp () {
62
66
repository = new JpaRepositoryFactory (em ).getRepository (SampleEntityRepository .class );
63
67
idClassRepository = new JpaRepositoryFactory (em ).getRepository (SampleWithIdClassRepository .class );
68
+ versionedUserRepository = new JpaRepositoryFactory (em ).getRepository (VersionedUserRepository .class );
64
69
}
65
70
66
71
@ Test
@@ -162,6 +167,26 @@ public Iterator<SampleEntityPK> iterator() {
162
167
assertThat (repository .findAll ()).containsExactly (two );
163
168
}
164
169
170
+ @ Test
171
+ void deleteStaleVersionedEntityShouldRaiseOptimisticLockException () {
172
+
173
+ VersionedUser entity = new VersionedUser ();
174
+ entity .setName ("name" );
175
+ versionedUserRepository .save (entity );
176
+ versionedUserRepository .flush ();
177
+ em .detach (entity );
178
+
179
+ versionedUserRepository .findById (entity .getId ()).ifPresent (u -> {
180
+ u .setName ("new name" );
181
+ VersionedUser latest = versionedUserRepository .save (u );
182
+ versionedUserRepository .flush ();
183
+ });
184
+
185
+ assertThatExceptionOfType (OptimisticLockException .class ).isThrownBy (() -> {
186
+ versionedUserRepository .delete (entity );
187
+ });
188
+ }
189
+
165
190
private interface SampleEntityRepository extends JpaRepository <SampleEntity , SampleEntityPK > {
166
191
167
192
}
@@ -170,4 +195,8 @@ private interface SampleWithIdClassRepository
170
195
extends CrudRepository <PersistableWithIdClass , PersistableWithIdClassPK > {
171
196
172
197
}
198
+
199
+ private interface VersionedUserRepository extends JpaRepository <VersionedUser , Long > {
200
+
201
+ }
173
202
}
0 commit comments