Skip to content

Commit

Permalink
Test deleting dirty detached and managed versioned entity
Browse files Browse the repository at this point in the history
  • Loading branch information
quaff committed Mar 21, 2024
1 parent a3cf247 commit 7abb5e7
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

/**
* @author Oliver Gierke
* @author Yanming Zhou
*/
@Entity
public class VersionedUser {
Expand All @@ -33,6 +34,8 @@ public class VersionedUser {
@Version
private Long version;

private String name;

public Long getId() {
return id;
}
Expand All @@ -48,4 +51,12 @@ public Long getVersion() {
public void setVersion(Long version) {
this.version = version;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,30 @@
import static org.assertj.core.api.Assertions.*;

import jakarta.persistence.EntityManager;
import jakarta.persistence.OptimisticLockException;
import jakarta.persistence.PersistenceContext;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.sample.PersistableWithIdClass;
import org.springframework.data.jpa.domain.sample.PersistableWithIdClassPK;
import org.springframework.data.jpa.domain.sample.SampleEntity;
import org.springframework.data.jpa.domain.sample.SampleEntityPK;
import org.springframework.data.jpa.domain.sample.VersionedUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -46,21 +54,28 @@
* @author Jens Schauder
* @author Greg Turnquist
* @author Krzysztof Krason
* @author Yanming Zhou
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration({ "classpath:infrastructure.xml" })
@Transactional
class JpaRepositoryTests {

@Autowired DataSource dataSource;

@PersistenceContext EntityManager em;

private JpaRepository<SampleEntity, SampleEntityPK> repository;
private CrudRepository<PersistableWithIdClass, PersistableWithIdClassPK> idClassRepository;
private JpaRepository<VersionedUser, Long> versionedUserRepository;
private NamedParameterJdbcOperations jdbcOperations;

@BeforeEach
void setUp() {
repository = new JpaRepositoryFactory(em).getRepository(SampleEntityRepository.class);
idClassRepository = new JpaRepositoryFactory(em).getRepository(SampleWithIdClassRepository.class);
versionedUserRepository = new JpaRepositoryFactory(em).getRepository(VersionedUserRepository.class);
jdbcOperations = new NamedParameterJdbcTemplate(dataSource);
}

@Test
Expand Down Expand Up @@ -162,6 +177,48 @@ public Iterator<SampleEntityPK> iterator() {
assertThat(repository.findAll()).containsExactly(two);
}

@Test
void deleteDirtyDetachedVersionedEntityShouldRaiseOptimisticLockException() {

VersionedUser entity = new VersionedUser();
entity.setName("name");
versionedUserRepository.save(entity);
versionedUserRepository.flush();
em.detach(entity);

versionedUserRepository.findById(entity.getId()).ifPresent(u -> {
u.setName("new name");
versionedUserRepository.flush();
});

assertThatExceptionOfType(OptimisticLockException.class).isThrownBy(() -> {
versionedUserRepository.delete(entity);
versionedUserRepository.flush();
});

jdbcOperations.update("delete from VersionedUser", Map.of());
}

@Test
void deleteDirtyManagedVersionedEntityShouldRaiseOptimisticLockException() {

VersionedUser entity = new VersionedUser();
entity.setName("name");
versionedUserRepository.save(entity);
versionedUserRepository.flush();


assertThat(jdbcOperations.update("update VersionedUser set version=version+1 where id=:id",
Map.of("id", entity.getId()))).isEqualTo(1);

assertThatExceptionOfType(OptimisticLockException.class).isThrownBy(() -> {
versionedUserRepository.delete(entity);
versionedUserRepository.flush();
});

jdbcOperations.update("delete from VersionedUser", Map.of());
}

private interface SampleEntityRepository extends JpaRepository<SampleEntity, SampleEntityPK> {

}
Expand All @@ -170,4 +227,8 @@ private interface SampleWithIdClassRepository
extends CrudRepository<PersistableWithIdClass, PersistableWithIdClassPK> {

}

private interface VersionedUserRepository extends JpaRepository<VersionedUser, Long> {

}
}

0 comments on commit 7abb5e7

Please sign in to comment.