Merge pull request #6350 from FDPro/bael-2538
[BAEL-2538] Spring Data JPA @Modifying annotation
This commit is contained in:
commit
6a68956032
@ -9,6 +9,7 @@ import org.springframework.data.jpa.repository.Modifying;
|
|||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.data.repository.query.Param;
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
@ -17,6 +18,9 @@ public interface UserRepository extends JpaRepository<User, Integer> , UserRepos
|
|||||||
|
|
||||||
Stream<User> findAllByName(String name);
|
Stream<User> findAllByName(String name);
|
||||||
|
|
||||||
|
@Query("select u from User u where u.email like '%@gmail.com'")
|
||||||
|
List<User> findUsersWithGmailAddress();
|
||||||
|
|
||||||
@Query("SELECT u FROM User u WHERE u.status = 1")
|
@Query("SELECT u FROM User u WHERE u.status = 1")
|
||||||
Collection<User> findAllActiveUsers();
|
Collection<User> findAllActiveUsers();
|
||||||
|
|
||||||
@ -67,14 +71,29 @@ public interface UserRepository extends JpaRepository<User, Integer> , UserRepos
|
|||||||
@Query(value = "UPDATE Users u SET u.status = ? WHERE u.name = ?", nativeQuery = true)
|
@Query(value = "UPDATE Users u SET u.status = ? WHERE u.name = ?", nativeQuery = true)
|
||||||
int updateUserSetStatusForNameNative(Integer status, String name);
|
int updateUserSetStatusForNameNative(Integer status, String name);
|
||||||
|
|
||||||
@Query(value = "INSERT INTO Users (name, age, email, status) VALUES (:name, :age, :email, :status)", nativeQuery = true)
|
@Query(value = "INSERT INTO Users (name, age, email, status, active) VALUES (:name, :age, :email, :status, :active)", nativeQuery = true)
|
||||||
@Modifying
|
@Modifying
|
||||||
void insertUser(@Param("name") String name, @Param("age") Integer age, @Param("status") Integer status, @Param("email") String email);
|
void insertUser(@Param("name") String name, @Param("age") Integer age, @Param("email") String email, @Param("status") Integer status, @Param("active") boolean active);
|
||||||
|
|
||||||
@Modifying
|
@Modifying
|
||||||
@Query(value = "UPDATE Users u SET status = ? WHERE u.name = ?", nativeQuery = true)
|
@Query(value = "UPDATE Users u SET status = ? WHERE u.name = ?", nativeQuery = true)
|
||||||
int updateUserSetStatusForNameNativePostgres(Integer status, String name);
|
int updateUserSetStatusForNameNativePostgres(Integer status, String name);
|
||||||
|
|
||||||
@Query(value = "SELECT u FROM User u WHERE u.name IN :names")
|
@Query(value = "SELECT u FROM User u WHERE u.name IN :names")
|
||||||
List<User> findUserByNameList(@Param("names") Collection<String> names);
|
List<User> findUserByNameList(@Param("names") Collection<String> names);
|
||||||
|
|
||||||
|
void deleteAllByCreationDateAfter(LocalDate date);
|
||||||
|
|
||||||
|
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||||
|
@Query("update User u set u.active = false where u.lastLoginDate < :date")
|
||||||
|
void deactivateUsersNotLoggedInSince(@Param("date") LocalDate date);
|
||||||
|
|
||||||
|
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||||
|
@Query("delete from User u where u.active = false")
|
||||||
|
int deleteDeactivatedUsers();
|
||||||
|
|
||||||
|
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||||
|
@Query(value = "alter table Users add column deleted int(1) not null default 0", nativeQuery = true)
|
||||||
|
void addDeletedColumn();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,14 @@
|
|||||||
package com.baeldung.dao.repositories.user;
|
package com.baeldung.dao.repositories.user;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
import com.baeldung.domain.user.User;
|
import com.baeldung.domain.user.User;
|
||||||
|
|
||||||
public interface UserRepositoryCustom {
|
public interface UserRepositoryCustom {
|
||||||
List<User> findUserByEmails(Set<String> emails);
|
List<User> findUserByEmails(Set<String> emails);
|
||||||
|
|
||||||
|
List<User> findAllUsersByPredicates(Collection<Predicate<User>> predicates);
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
package com.baeldung.dao.repositories.user;
|
package com.baeldung.dao.repositories.user;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.PersistenceContext;
|
import javax.persistence.PersistenceContext;
|
||||||
@ -40,4 +43,15 @@ public class UserRepositoryCustomImpl implements UserRepositoryCustom {
|
|||||||
.getResultList();
|
.getResultList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<User> findAllUsersByPredicates(Collection<java.util.function.Predicate<User>> predicates) {
|
||||||
|
List<User> allUsers = entityManager.createQuery("select u from User u", User.class).getResultList();
|
||||||
|
Stream<User> allUsersStream = allUsers.stream();
|
||||||
|
for (java.util.function.Predicate<User> predicate : predicates) {
|
||||||
|
allUsersStream = allUsersStream.filter(predicate);
|
||||||
|
}
|
||||||
|
|
||||||
|
return allUsersStream.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package com.baeldung.domain.user;
|
package com.baeldung.domain.user;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
|
import java.time.LocalDate;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "users")
|
@Table(name = "users")
|
||||||
@ -11,6 +13,9 @@ public class User {
|
|||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private int id;
|
private int id;
|
||||||
private String name;
|
private String name;
|
||||||
|
private LocalDate creationDate;
|
||||||
|
private LocalDate lastLoginDate;
|
||||||
|
private boolean active;
|
||||||
private int age;
|
private int age;
|
||||||
@Column(unique = true, nullable = false)
|
@Column(unique = true, nullable = false)
|
||||||
private String email;
|
private String email;
|
||||||
@ -22,10 +27,12 @@ public class User {
|
|||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
public User(String name, String email, Integer status) {
|
public User(String name, LocalDate creationDate, String email, Integer status) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
this.creationDate = creationDate;
|
||||||
this.email = email;
|
this.email = email;
|
||||||
this.status = status;
|
this.status = status;
|
||||||
|
this.active = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getId() {
|
public int getId() {
|
||||||
@ -44,6 +51,10 @@ public class User {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public LocalDate getCreationDate() {
|
||||||
|
return creationDate;
|
||||||
|
}
|
||||||
|
|
||||||
public String getEmail() {
|
public String getEmail() {
|
||||||
return email;
|
return email;
|
||||||
}
|
}
|
||||||
@ -83,4 +94,37 @@ public class User {
|
|||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
User user = (User) o;
|
||||||
|
return id == user.id &&
|
||||||
|
age == user.age &&
|
||||||
|
Objects.equals(name, user.name) &&
|
||||||
|
Objects.equals(creationDate, user.creationDate) &&
|
||||||
|
Objects.equals(email, user.email) &&
|
||||||
|
Objects.equals(status, user.status);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(id, name, creationDate, age, email, status);
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDate getLastLoginDate() {
|
||||||
|
return lastLoginDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastLoginDate(LocalDate lastLoginDate) {
|
||||||
|
this.lastLoginDate = lastLoginDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isActive() {
|
||||||
|
return active;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setActive(boolean active) {
|
||||||
|
this.active = active;
|
||||||
|
}
|
||||||
}
|
}
|
@ -12,10 +12,15 @@ import org.springframework.data.jpa.domain.JpaSort;
|
|||||||
import org.springframework.data.mapping.PropertyReferenceException;
|
import org.springframework.data.mapping.PropertyReferenceException;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.Query;
|
||||||
|
import java.time.LocalDate;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.function.Predicate;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
class UserRepositoryCommon {
|
class UserRepositoryCommon {
|
||||||
|
|
||||||
@ -32,6 +37,8 @@ class UserRepositoryCommon {
|
|||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
protected UserRepository userRepository;
|
protected UserRepository userRepository;
|
||||||
|
@Autowired
|
||||||
|
private EntityManager entityManager;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Transactional
|
@Transactional
|
||||||
@ -254,9 +261,9 @@ class UserRepositoryCommon {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUsersInDB_WhenFindAllWithSortByName_ThenReturnUsersSorted() {
|
public void givenUsersInDB_WhenFindAllWithSortByName_ThenReturnUsersSorted() {
|
||||||
userRepository.save(new User(USER_NAME_ADAM, USER_EMAIL, ACTIVE_STATUS));
|
userRepository.save(new User(USER_NAME_ADAM, LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
|
||||||
userRepository.save(new User(USER_NAME_PETER, USER_EMAIL2, ACTIVE_STATUS));
|
userRepository.save(new User(USER_NAME_PETER, LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, INACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, INACTIVE_STATUS));
|
||||||
|
|
||||||
List<User> usersSortByName = userRepository.findAll(new Sort(Sort.Direction.ASC, "name"));
|
List<User> usersSortByName = userRepository.findAll(new Sort(Sort.Direction.ASC, "name"));
|
||||||
|
|
||||||
@ -266,9 +273,9 @@ class UserRepositoryCommon {
|
|||||||
|
|
||||||
@Test(expected = PropertyReferenceException.class)
|
@Test(expected = PropertyReferenceException.class)
|
||||||
public void givenUsersInDB_WhenFindAllSortWithFunction_ThenThrowException() {
|
public void givenUsersInDB_WhenFindAllSortWithFunction_ThenThrowException() {
|
||||||
userRepository.save(new User(USER_NAME_ADAM, USER_EMAIL, ACTIVE_STATUS));
|
userRepository.save(new User(USER_NAME_ADAM, LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
|
||||||
userRepository.save(new User(USER_NAME_PETER, USER_EMAIL2, ACTIVE_STATUS));
|
userRepository.save(new User(USER_NAME_PETER, LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, INACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, INACTIVE_STATUS));
|
||||||
|
|
||||||
userRepository.findAll(new Sort(Sort.Direction.ASC, "name"));
|
userRepository.findAll(new Sort(Sort.Direction.ASC, "name"));
|
||||||
|
|
||||||
@ -280,9 +287,9 @@ class UserRepositoryCommon {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUsersInDB_WhenFindAllSortWithFunctionQueryAnnotationJPQL_ThenReturnUsersSorted() {
|
public void givenUsersInDB_WhenFindAllSortWithFunctionQueryAnnotationJPQL_ThenReturnUsersSorted() {
|
||||||
userRepository.save(new User(USER_NAME_ADAM, USER_EMAIL, ACTIVE_STATUS));
|
userRepository.save(new User(USER_NAME_ADAM, LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
|
||||||
userRepository.save(new User(USER_NAME_PETER, USER_EMAIL2, ACTIVE_STATUS));
|
userRepository.save(new User(USER_NAME_PETER, LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, INACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, INACTIVE_STATUS));
|
||||||
|
|
||||||
userRepository.findAllUsers(new Sort("name"));
|
userRepository.findAllUsers(new Sort("name"));
|
||||||
|
|
||||||
@ -294,12 +301,12 @@ class UserRepositoryCommon {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUsersInDB_WhenFindAllWithPageRequestQueryAnnotationJPQL_ThenReturnPageOfUsers() {
|
public void givenUsersInDB_WhenFindAllWithPageRequestQueryAnnotationJPQL_ThenReturnPageOfUsers() {
|
||||||
userRepository.save(new User(USER_NAME_ADAM, USER_EMAIL, ACTIVE_STATUS));
|
userRepository.save(new User(USER_NAME_ADAM, LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
|
||||||
userRepository.save(new User(USER_NAME_PETER, USER_EMAIL2, ACTIVE_STATUS));
|
userRepository.save(new User(USER_NAME_PETER, LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, INACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, INACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE1", USER_EMAIL4, INACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE1", LocalDate.now(), USER_EMAIL4, INACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE2", USER_EMAIL5, INACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE2", LocalDate.now(), USER_EMAIL5, INACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE3", USER_EMAIL6, INACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE3", LocalDate.now(), USER_EMAIL6, INACTIVE_STATUS));
|
||||||
|
|
||||||
Page<User> usersPage = userRepository.findAllUsersWithPagination(new PageRequest(1, 3));
|
Page<User> usersPage = userRepository.findAllUsersWithPagination(new PageRequest(1, 3));
|
||||||
|
|
||||||
@ -310,27 +317,27 @@ class UserRepositoryCommon {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUsersInDB_WhenFindAllWithPageRequestQueryAnnotationNative_ThenReturnPageOfUsers() {
|
public void givenUsersInDB_WhenFindAllWithPageRequestQueryAnnotationNative_ThenReturnPageOfUsers() {
|
||||||
userRepository.save(new User(USER_NAME_ADAM, USER_EMAIL, ACTIVE_STATUS));
|
userRepository.save(new User(USER_NAME_ADAM, LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
|
||||||
userRepository.save(new User(USER_NAME_PETER, USER_EMAIL2, ACTIVE_STATUS));
|
userRepository.save(new User(USER_NAME_PETER, LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, INACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, INACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE1", USER_EMAIL4, INACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE1", LocalDate.now(), USER_EMAIL4, INACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE2", USER_EMAIL5, INACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE2", LocalDate.now(), USER_EMAIL5, INACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE3", USER_EMAIL6, INACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE3", LocalDate.now(), USER_EMAIL6, INACTIVE_STATUS));
|
||||||
|
|
||||||
Page<User> usersSortByNameLength = userRepository.findAllUsersWithPaginationNative(new PageRequest(1, 3));
|
Page<User> usersSortByNameLength = userRepository.findAllUsersWithPaginationNative(new PageRequest(1, 3));
|
||||||
|
|
||||||
assertThat(usersSortByNameLength.getContent()
|
assertThat(usersSortByNameLength.getContent()
|
||||||
.get(0)
|
.get(0)
|
||||||
.getName()).isEqualTo("SAMPLE1");
|
.getName()).isEqualTo(USER_NAME_PETER);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Transactional
|
@Transactional
|
||||||
public void givenUsersInDB_WhenUpdateStatusForNameModifyingQueryAnnotationJPQL_ThenModifyMatchingUsers() {
|
public void givenUsersInDB_WhenUpdateStatusForNameModifyingQueryAnnotationJPQL_ThenModifyMatchingUsers() {
|
||||||
userRepository.save(new User("SAMPLE", USER_EMAIL, ACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE1", USER_EMAIL2, ACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE1", LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, ACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE3", USER_EMAIL4, ACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE3", LocalDate.now(), USER_EMAIL4, ACTIVE_STATUS));
|
||||||
|
|
||||||
int updatedUsersSize = userRepository.updateUserSetStatusForName(INACTIVE_STATUS, "SAMPLE");
|
int updatedUsersSize = userRepository.updateUserSetStatusForName(INACTIVE_STATUS, "SAMPLE");
|
||||||
|
|
||||||
@ -385,8 +392,8 @@ class UserRepositoryCommon {
|
|||||||
@Test
|
@Test
|
||||||
@Transactional
|
@Transactional
|
||||||
public void whenInsertedWithQuery_ThenUserIsPersisted() {
|
public void whenInsertedWithQuery_ThenUserIsPersisted() {
|
||||||
userRepository.insertUser(USER_NAME_ADAM, 1, ACTIVE_STATUS, USER_EMAIL);
|
userRepository.insertUser(USER_NAME_ADAM, 1, USER_EMAIL, ACTIVE_STATUS, true);
|
||||||
userRepository.insertUser(USER_NAME_PETER, 1, ACTIVE_STATUS, USER_EMAIL2);
|
userRepository.insertUser(USER_NAME_PETER, 1, USER_EMAIL2, ACTIVE_STATUS, true);
|
||||||
|
|
||||||
User userAdam = userRepository.findUserByNameLike(USER_NAME_ADAM);
|
User userAdam = userRepository.findUserByNameLike(USER_NAME_ADAM);
|
||||||
User userPeter = userRepository.findUserByNameLike(USER_NAME_PETER);
|
User userPeter = userRepository.findUserByNameLike(USER_NAME_PETER);
|
||||||
@ -396,7 +403,140 @@ class UserRepositoryCommon {
|
|||||||
assertThat(userPeter).isNotNull();
|
assertThat(userPeter).isNotNull();
|
||||||
assertThat(userPeter.getEmail()).isEqualTo(USER_EMAIL2);
|
assertThat(userPeter.getEmail()).isEqualTo(USER_EMAIL2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Transactional
|
||||||
|
public void givenTwoUsers_whenFindByNameUsr01_ThenUserUsr01() {
|
||||||
|
User usr01 = new User("usr01", LocalDate.now(), "usr01@baeldung.com", 1);
|
||||||
|
User usr02 = new User("usr02", LocalDate.now(), "usr02@baeldung.com", 1);
|
||||||
|
|
||||||
|
userRepository.save(usr01);
|
||||||
|
userRepository.save(usr02);
|
||||||
|
|
||||||
|
try (Stream<User> users = userRepository.findAllByName("usr01")) {
|
||||||
|
assertTrue(users.allMatch(usr -> usr.equals(usr01)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Transactional
|
||||||
|
public void givenTwoUsers_whenFindByNameUsr00_ThenNoUsers() {
|
||||||
|
User usr01 = new User("usr01", LocalDate.now(), "usr01@baeldung.com", 1);
|
||||||
|
User usr02 = new User("usr02", LocalDate.now(), "usr02@baeldung.com", 1);
|
||||||
|
|
||||||
|
userRepository.save(usr01);
|
||||||
|
userRepository.save(usr02);
|
||||||
|
|
||||||
|
try (Stream<User> users = userRepository.findAllByName("usr00")) {
|
||||||
|
assertEquals(0, users.count());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTwoUsers_whenFindUsersWithGmailAddress_ThenUserUsr02() {
|
||||||
|
User usr01 = new User("usr01", LocalDate.now(), "usr01@baeldung.com", 1);
|
||||||
|
User usr02 = new User("usr02", LocalDate.now(), "usr02@gmail.com", 1);
|
||||||
|
|
||||||
|
userRepository.save(usr01);
|
||||||
|
userRepository.save(usr02);
|
||||||
|
|
||||||
|
List<User> users = userRepository.findUsersWithGmailAddress();
|
||||||
|
assertEquals(1, users.size());
|
||||||
|
assertEquals(usr02, users.get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Transactional
|
||||||
|
public void givenTwoUsers_whenDeleteAllByCreationDateAfter_ThenOneUserRemains() {
|
||||||
|
User usr01 = new User("usr01", LocalDate.of(2018, 1, 1), "usr01@baeldung.com", 1);
|
||||||
|
User usr02 = new User("usr02", LocalDate.of(2018, 6, 1), "usr02@baeldung.com", 1);
|
||||||
|
|
||||||
|
userRepository.save(usr01);
|
||||||
|
userRepository.save(usr02);
|
||||||
|
|
||||||
|
userRepository.deleteAllByCreationDateAfter(LocalDate.of(2018, 5, 1));
|
||||||
|
|
||||||
|
List<User> users = userRepository.findAll();
|
||||||
|
|
||||||
|
assertEquals(1, users.size());
|
||||||
|
assertEquals(usr01, users.get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTwoUsers_whenFindAllUsersByPredicates_ThenUserUsr01() {
|
||||||
|
User usr01 = new User("usr01", LocalDate.of(2018, 1, 1), "usr01@baeldung.com", 1);
|
||||||
|
User usr02 = new User("usr02", LocalDate.of(2018, 6, 1), "usr02@baeldung.org", 1);
|
||||||
|
|
||||||
|
userRepository.save(usr01);
|
||||||
|
userRepository.save(usr02);
|
||||||
|
|
||||||
|
List<Predicate<User>> predicates = new ArrayList<>();
|
||||||
|
predicates.add(usr -> usr.getCreationDate().isAfter(LocalDate.of(2017, 12, 31)));
|
||||||
|
predicates.add(usr -> usr.getEmail().endsWith(".com"));
|
||||||
|
|
||||||
|
List<User> users = userRepository.findAllUsersByPredicates(predicates);
|
||||||
|
|
||||||
|
assertEquals(1, users.size());
|
||||||
|
assertEquals(usr01, users.get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Transactional
|
||||||
|
public void givenTwoUsers_whenDeactivateUsersNotLoggedInSince_ThenUserUsr02Deactivated() {
|
||||||
|
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.org", 1);
|
||||||
|
usr02.setLastLoginDate(LocalDate.of(2018, 7, 20));
|
||||||
|
|
||||||
|
userRepository.save(usr01);
|
||||||
|
userRepository.save(usr02);
|
||||||
|
|
||||||
|
userRepository.deactivateUsersNotLoggedInSince(LocalDate.of(2018, 8, 1));
|
||||||
|
|
||||||
|
List<User> users = userRepository.findAllUsers(Sort.by(Sort.Order.asc("name")));
|
||||||
|
assertTrue(users.get(0).isActive());
|
||||||
|
assertFalse(users.get(1).isActive());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Transactional
|
||||||
|
public void givenTwoUsers_whenDeleteDeactivatedUsers_ThenUserUsr02Deleted() {
|
||||||
|
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);
|
||||||
|
|
||||||
|
int deletedUsersCount = userRepository.deleteDeactivatedUsers();
|
||||||
|
|
||||||
|
List<User> users = userRepository.findAll();
|
||||||
|
assertEquals(1, users.size());
|
||||||
|
assertEquals(usr01, users.get(0));
|
||||||
|
assertEquals(1, deletedUsersCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Transactional
|
||||||
|
public void givenTwoUsers_whenAddDeletedColumn_ThenUsersHaveDeletedColumn() {
|
||||||
|
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.org", 1);
|
||||||
|
usr02.setLastLoginDate(LocalDate.of(2018, 7, 20));
|
||||||
|
usr02.setActive(false);
|
||||||
|
|
||||||
|
userRepository.save(usr01);
|
||||||
|
userRepository.save(usr02);
|
||||||
|
|
||||||
|
userRepository.addDeletedColumn();
|
||||||
|
|
||||||
|
Query nativeQuery = entityManager.createNativeQuery("select deleted from USERS where NAME = 'usr01'");
|
||||||
|
assertEquals(0, nativeQuery.getResultList().get(0));
|
||||||
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void cleanUp() {
|
public void cleanUp() {
|
||||||
userRepository.deleteAll();
|
userRepository.deleteAll();
|
||||||
|
@ -9,6 +9,8 @@ import org.springframework.test.annotation.DirtiesContext;
|
|||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -22,10 +24,10 @@ public class UserRepositoryIntegrationTest extends UserRepositoryCommon {
|
|||||||
@Test
|
@Test
|
||||||
@Transactional
|
@Transactional
|
||||||
public void givenUsersInDBWhenUpdateStatusForNameModifyingQueryAnnotationNativeThenModifyMatchingUsers() {
|
public void givenUsersInDBWhenUpdateStatusForNameModifyingQueryAnnotationNativeThenModifyMatchingUsers() {
|
||||||
userRepository.save(new User("SAMPLE", USER_EMAIL, ACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE1", USER_EMAIL2, ACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE1", LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, ACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE3", USER_EMAIL4, ACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE3", LocalDate.now(), USER_EMAIL4, ACTIVE_STATUS));
|
||||||
userRepository.flush();
|
userRepository.flush();
|
||||||
|
|
||||||
int updatedUsersSize = userRepository.updateUserSetStatusForNameNative(INACTIVE_STATUS, "SAMPLE");
|
int updatedUsersSize = userRepository.updateUserSetStatusForNameNative(INACTIVE_STATUS, "SAMPLE");
|
||||||
|
@ -11,6 +11,8 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.testcontainers.containers.PostgreSQLContainer;
|
import org.testcontainers.containers.PostgreSQLContainer;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -27,10 +29,10 @@ public class UserRepositoryTCAutoIntegrationTest extends UserRepositoryCommon {
|
|||||||
@Test
|
@Test
|
||||||
@Transactional
|
@Transactional
|
||||||
public void givenUsersInDB_WhenUpdateStatusForNameModifyingQueryAnnotationNativePostgres_ThenModifyMatchingUsers() {
|
public void givenUsersInDB_WhenUpdateStatusForNameModifyingQueryAnnotationNativePostgres_ThenModifyMatchingUsers() {
|
||||||
userRepository.save(new User("SAMPLE", USER_EMAIL, ACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE1", USER_EMAIL2, ACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE1", LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, ACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE3", USER_EMAIL4, ACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE3", LocalDate.now(), USER_EMAIL4, ACTIVE_STATUS));
|
||||||
userRepository.flush();
|
userRepository.flush();
|
||||||
|
|
||||||
int updatedUsersSize = userRepository.updateUserSetStatusForNameNativePostgres(INACTIVE_STATUS, "SAMPLE");
|
int updatedUsersSize = userRepository.updateUserSetStatusForNameNativePostgres(INACTIVE_STATUS, "SAMPLE");
|
||||||
|
@ -14,6 +14,8 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.testcontainers.containers.PostgreSQLContainer;
|
import org.testcontainers.containers.PostgreSQLContainer;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@ -31,10 +33,10 @@ public class UserRepositoryTCIntegrationTest extends UserRepositoryCommon {
|
|||||||
@Test
|
@Test
|
||||||
@Transactional
|
@Transactional
|
||||||
public void givenUsersInDB_WhenUpdateStatusForNameModifyingQueryAnnotationNative_ThenModifyMatchingUsers() {
|
public void givenUsersInDB_WhenUpdateStatusForNameModifyingQueryAnnotationNative_ThenModifyMatchingUsers() {
|
||||||
userRepository.save(new User("SAMPLE", USER_EMAIL, ACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE1", USER_EMAIL2, ACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE1", LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, ACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE3", USER_EMAIL4, ACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE3", LocalDate.now(), USER_EMAIL4, ACTIVE_STATUS));
|
||||||
userRepository.flush();
|
userRepository.flush();
|
||||||
|
|
||||||
int updatedUsersSize = userRepository.updateUserSetStatusForNameNativePostgres(INACTIVE_STATUS, "SAMPLE");
|
int updatedUsersSize = userRepository.updateUserSetStatusForNameNativePostgres(INACTIVE_STATUS, "SAMPLE");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user