Skip to content

Commit ac3f819

Browse files
committed
Test deleting versioned entity
1 parent a3cf247 commit ac3f819

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

spring-data-jpa/src/test/java/org/springframework/data/jpa/domain/sample/VersionedUser.java

+11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
/**
2424
* @author Oliver Gierke
25+
* @author Yanming Zhou
2526
*/
2627
@Entity
2728
public class VersionedUser {
@@ -33,6 +34,8 @@ public class VersionedUser {
3334
@Version
3435
private Long version;
3536

37+
private String name;
38+
3639
public Long getId() {
3740
return id;
3841
}
@@ -48,4 +51,12 @@ public Long getVersion() {
4851
public void setVersion(Long version) {
4952
this.version = version;
5053
}
54+
55+
public String getName() {
56+
return name;
57+
}
58+
59+
public void setName(String name) {
60+
this.name = name;
61+
}
5162
}

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/support/JpaRepositoryTests.java

+29
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.assertj.core.api.Assertions.*;
1919

2020
import jakarta.persistence.EntityManager;
21+
import jakarta.persistence.OptimisticLockException;
2122
import jakarta.persistence.PersistenceContext;
2223

2324
import java.util.Arrays;
@@ -32,6 +33,7 @@
3233
import org.springframework.data.jpa.domain.sample.PersistableWithIdClassPK;
3334
import org.springframework.data.jpa.domain.sample.SampleEntity;
3435
import org.springframework.data.jpa.domain.sample.SampleEntityPK;
36+
import org.springframework.data.jpa.domain.sample.VersionedUser;
3537
import org.springframework.data.jpa.repository.JpaRepository;
3638
import org.springframework.data.repository.CrudRepository;
3739
import org.springframework.test.context.ContextConfiguration;
@@ -46,6 +48,7 @@
4648
* @author Jens Schauder
4749
* @author Greg Turnquist
4850
* @author Krzysztof Krason
51+
* @author Yanming Zhou
4952
*/
5053
@ExtendWith(SpringExtension.class)
5154
@ContextConfiguration({ "classpath:infrastructure.xml" })
@@ -56,11 +59,13 @@ class JpaRepositoryTests {
5659

5760
private JpaRepository<SampleEntity, SampleEntityPK> repository;
5861
private CrudRepository<PersistableWithIdClass, PersistableWithIdClassPK> idClassRepository;
62+
private JpaRepository<VersionedUser, Long> versionedUserRepository;
5963

6064
@BeforeEach
6165
void setUp() {
6266
repository = new JpaRepositoryFactory(em).getRepository(SampleEntityRepository.class);
6367
idClassRepository = new JpaRepositoryFactory(em).getRepository(SampleWithIdClassRepository.class);
68+
versionedUserRepository = new JpaRepositoryFactory(em).getRepository(VersionedUserRepository.class);
6469
}
6570

6671
@Test
@@ -162,6 +167,26 @@ public Iterator<SampleEntityPK> iterator() {
162167
assertThat(repository.findAll()).containsExactly(two);
163168
}
164169

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+
165190
private interface SampleEntityRepository extends JpaRepository<SampleEntity, SampleEntityPK> {
166191

167192
}
@@ -170,4 +195,8 @@ private interface SampleWithIdClassRepository
170195
extends CrudRepository<PersistableWithIdClass, PersistableWithIdClassPK> {
171196

172197
}
198+
199+
private interface VersionedUserRepository extends JpaRepository<VersionedUser, Long> {
200+
201+
}
173202
}

0 commit comments

Comments
 (0)