BAEL-4194: Add example for not using the @Modifying (#10536)
This commit is contained in:
parent
1c32920b76
commit
28cd4fa7ba
|
@ -6,7 +6,6 @@ This module contains articles about annotations used in Spring Data JPA
|
|||
|
||||
- [DDD Aggregates and @DomainEvents](https://www.baeldung.com/spring-data-ddd)
|
||||
- [JPA @Embedded And @Embeddable](https://www.baeldung.com/jpa-embedded-embeddable)
|
||||
- [Spring Data JPA @Modifying Annotation](https://www.baeldung.com/spring-data-jpa-modifying-annotation)
|
||||
- [Spring JPA @Embedded and @EmbeddedId](https://www.baeldung.com/spring-jpa-embedded-method-parameters)
|
||||
- [Programmatic Transaction Management in Spring](https://www.baeldung.com/spring-programmatic-transaction-management)
|
||||
- [JPA Entity Lifecycle Events](https://www.baeldung.com/jpa-entity-lifecycle-events)
|
||||
|
|
|
@ -12,6 +12,7 @@ This module contains articles about Spring Data JPA used in enterprise applicati
|
|||
- [Working with Lazy Element Collections in JPA](https://www.baeldung.com/java-jpa-lazy-collections)
|
||||
- [Custom Naming Convention with Spring Data JPA](https://www.baeldung.com/spring-data-jpa-custom-naming)
|
||||
- [Partial Data Update with Spring Data](https://www.baeldung.com/spring-data-partial-update)
|
||||
- [Spring Data JPA @Modifying Annotation](https://www.baeldung.com/spring-data-jpa-modifying-annotation)
|
||||
|
||||
### Eclipse Config
|
||||
After importing the project into Eclipse, you may see the following error:
|
||||
|
|
|
@ -93,6 +93,9 @@ public interface UserRepository extends JpaRepository<User, Integer> , UserRepos
|
|||
@Query("delete User u where u.active = false")
|
||||
int deleteDeactivatedUsers();
|
||||
|
||||
@Query("delete User u where u.active = false")
|
||||
int deleteDeactivatedUsersWithNoModifyingAnnotation();
|
||||
|
||||
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||
@Query(value = "alter table USERS add column deleted int(1) not null default 0", nativeQuery = true)
|
||||
void addDeletedColumn();
|
||||
|
|
|
@ -3,6 +3,7 @@ package com.baeldung.boot.daos;
|
|||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
|
@ -21,6 +22,7 @@ import java.util.function.Predicate;
|
|||
import java.util.stream.Stream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class UserRepositoryCommon {
|
||||
|
@ -520,6 +522,22 @@ public class UserRepositoryCommon {
|
|||
assertEquals(1, deletedUsersCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenTwoUsers_whenDeleteDeactivatedUsersWithNoModifyingAnnotation_ThenException() {
|
||||
User usr01 = new User("usr01", LocalDate.of(2018, 1, 1), "usr01@baeldung.com", 1);
|
||||
usr01.setLastLoginDate(LocalDate.now());
|
||||
User usr02 = new User("usr02", LocalDate.of(2018, 6, 1), "usr02@baeldung.com", 0);
|
||||
usr02.setLastLoginDate(LocalDate.of(2018, 7, 20));
|
||||
usr02.setActive(false);
|
||||
|
||||
userRepository.save(usr01);
|
||||
userRepository.save(usr02);
|
||||
|
||||
assertThatThrownBy(() -> userRepository.deleteDeactivatedUsersWithNoModifyingAnnotation())
|
||||
.isInstanceOf(InvalidDataAccessApiUsageException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenTwoUsers_whenAddDeletedColumn_ThenUsersHaveDeletedColumn() {
|
||||
|
|
Loading…
Reference in New Issue