refactor spring-data-jpa
This commit is contained in:
+2
-14
@@ -1,28 +1,16 @@
|
||||
package com.baeldung.boot.config;
|
||||
|
||||
import com.baeldung.boot.daos.impl.ExtendedRepositoryImpl;
|
||||
import com.baeldung.boot.services.IBarService;
|
||||
import com.baeldung.boot.services.impl.BarSpringDataJpaService;
|
||||
import com.google.common.base.Preconditions;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.*;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.JpaVendorAdapter;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.Properties;
|
||||
|
||||
@Configuration
|
||||
@Profile("!tc")
|
||||
@EnableTransactionManagement
|
||||
@EnableJpaAuditing
|
||||
public class PersistenceConfiguration {
|
||||
|
||||
@Bean
|
||||
|
||||
+2
-2
@@ -1,8 +1,8 @@
|
||||
package com.baeldung.batchinserts.repository;
|
||||
package com.baeldung.boot.daos;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import com.baeldung.batchinserts.model.Customer;
|
||||
import com.baeldung.boot.domain.Customer;
|
||||
|
||||
/**
|
||||
* JPA CrudRepository interface
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.boot.user;
|
||||
package com.baeldung.boot.daos.user;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
+12
-12
@@ -1,6 +1,5 @@
|
||||
package com.baeldung.dao.repositories.user;
|
||||
package com.baeldung.boot.daos.user;
|
||||
|
||||
import com.baeldung.domain.user.User;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
@@ -9,6 +8,8 @@ import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import com.baeldung.boot.domain.User;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
@@ -18,11 +19,11 @@ public interface UserRepository extends JpaRepository<User, Integer> , UserRepos
|
||||
|
||||
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")
|
||||
Collection<User> findAllActiveUsers();
|
||||
|
||||
@Query("select u from User u where u.email like '%@gmail.com'")
|
||||
List<User> findUsersWithGmailAddress();
|
||||
|
||||
@Query(value = "SELECT * FROM Users u WHERE u.status = 1", nativeQuery = true)
|
||||
Collection<User> findAllActiveUsersNative();
|
||||
@@ -74,14 +75,14 @@ public interface UserRepository extends JpaRepository<User, Integer> , UserRepos
|
||||
@Query(value = "INSERT INTO Users (name, age, email, status, active) VALUES (:name, :age, :email, :status, :active)", nativeQuery = true)
|
||||
@Modifying
|
||||
void insertUser(@Param("name") String name, @Param("age") Integer age, @Param("email") String email, @Param("status") Integer status, @Param("active") boolean active);
|
||||
|
||||
|
||||
@Modifying
|
||||
@Query(value = "UPDATE Users u SET status = ? WHERE u.name = ?", nativeQuery = true)
|
||||
int updateUserSetStatusForNameNativePostgres(Integer status, String name);
|
||||
|
||||
|
||||
@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)
|
||||
@@ -89,11 +90,10 @@ public interface UserRepository extends JpaRepository<User, Integer> , UserRepos
|
||||
void deactivateUsersNotLoggedInSince(@Param("date") LocalDate date);
|
||||
|
||||
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||
@Query("delete from User u where u.active = false")
|
||||
@Query("delete 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)
|
||||
@Query(value = "alter table USERS.USERS add column deleted int(1) not null default 0", nativeQuery = true)
|
||||
void addDeletedColumn();
|
||||
|
||||
}
|
||||
+3
-3
@@ -1,14 +1,14 @@
|
||||
package com.baeldung.dao.repositories.user;
|
||||
package com.baeldung.boot.daos.user;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import com.baeldung.domain.user.User;
|
||||
import com.baeldung.boot.domain.User;
|
||||
|
||||
public interface UserRepositoryCustom {
|
||||
List<User> findUserByEmails(Set<String> emails);
|
||||
|
||||
|
||||
List<User> findAllUsersByPredicates(Collection<Predicate<User>> predicates);
|
||||
}
|
||||
+3
-3
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.dao.repositories.user;
|
||||
package com.baeldung.boot.daos.user;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@@ -15,7 +15,7 @@ import javax.persistence.criteria.Path;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import com.baeldung.domain.user.User;
|
||||
import com.baeldung.boot.domain.User;
|
||||
|
||||
public class UserRepositoryCustomImpl implements UserRepositoryCustom {
|
||||
|
||||
@@ -42,7 +42,7 @@ public class UserRepositoryCustomImpl implements UserRepositoryCustom {
|
||||
return entityManager.createQuery(query)
|
||||
.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();
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.batchinserts.model;
|
||||
package com.baeldung.boot.domain;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
+1
@@ -1,6 +1,7 @@
|
||||
package com.baeldung.boot.domain;
|
||||
|
||||
import javax.persistence.*;
|
||||
import com.baeldung.boot.domain.Possession;
|
||||
|
||||
@Entity
|
||||
@Table
|
||||
|
||||
+47
-1
@@ -1,7 +1,10 @@
|
||||
package com.baeldung.boot.domain;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
@@ -11,6 +14,9 @@ public class User {
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private int id;
|
||||
private String name;
|
||||
private LocalDate creationDate;
|
||||
private LocalDate lastLoginDate;
|
||||
private boolean active;
|
||||
private int age;
|
||||
@Column(unique = true, nullable = false)
|
||||
private String email;
|
||||
@@ -22,10 +28,12 @@ public class User {
|
||||
super();
|
||||
}
|
||||
|
||||
public User(String name, String email, Integer status) {
|
||||
public User(String name, LocalDate creationDate,String email, Integer status) {
|
||||
this.name = name;
|
||||
this.creationDate = creationDate;
|
||||
this.email = email;
|
||||
this.status = status;
|
||||
this.active = true;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
@@ -67,6 +75,10 @@ public class User {
|
||||
public void setAge(final int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public LocalDate getCreationDate() {
|
||||
return creationDate;
|
||||
}
|
||||
|
||||
public List<Possession> getPossessionList() {
|
||||
return possessionList;
|
||||
@@ -82,5 +94,39 @@ public class User {
|
||||
builder.append("User [name=").append(name).append(", id=").append(id).append("]");
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
-81
@@ -1,81 +0,0 @@
|
||||
package com.baeldung.boot.user;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import com.baeldung.boot.domain.User;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public interface UserRepository extends JpaRepository<User, Integer> , UserRepositoryCustom{
|
||||
|
||||
Stream<User> findAllByName(String name);
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.status = 1")
|
||||
Collection<User> findAllActiveUsers();
|
||||
|
||||
@Query(value = "SELECT * FROM Users u WHERE u.status = 1", nativeQuery = true)
|
||||
Collection<User> findAllActiveUsersNative();
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.status = ?1")
|
||||
User findUserByStatus(Integer status);
|
||||
|
||||
@Query(value = "SELECT * FROM Users u WHERE u.status = ?1", nativeQuery = true)
|
||||
User findUserByStatusNative(Integer status);
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.status = ?1 and u.name = ?2")
|
||||
User findUserByStatusAndName(Integer status, String name);
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.status = :status and u.name = :name")
|
||||
User findUserByStatusAndNameNamedParams(@Param("status") Integer status, @Param("name") String name);
|
||||
|
||||
@Query(value = "SELECT * FROM Users u WHERE u.status = :status AND u.name = :name", nativeQuery = true)
|
||||
User findUserByStatusAndNameNamedParamsNative(@Param("status") Integer status, @Param("name") String name);
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.status = :status and u.name = :name")
|
||||
User findUserByUserStatusAndUserName(@Param("status") Integer userStatus, @Param("name") String userName);
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.name like ?1%")
|
||||
User findUserByNameLike(String name);
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.name like :name%")
|
||||
User findUserByNameLikeNamedParam(@Param("name") String name);
|
||||
|
||||
@Query(value = "SELECT * FROM users u WHERE u.name LIKE ?1%", nativeQuery = true)
|
||||
User findUserByNameLikeNative(String name);
|
||||
|
||||
@Query(value = "SELECT u FROM User u")
|
||||
List<User> findAllUsers(Sort sort);
|
||||
|
||||
@Query(value = "SELECT u FROM User u ORDER BY id")
|
||||
Page<User> findAllUsersWithPagination(Pageable pageable);
|
||||
|
||||
@Query(value = "SELECT * FROM Users ORDER BY id", countQuery = "SELECT count(*) FROM Users", nativeQuery = true)
|
||||
Page<User> findAllUsersWithPaginationNative(Pageable pageable);
|
||||
|
||||
@Modifying
|
||||
@Query("update User u set u.status = :status where u.name = :name")
|
||||
int updateUserSetStatusForName(@Param("status") Integer status, @Param("name") String name);
|
||||
|
||||
@Modifying
|
||||
@Query(value = "UPDATE Users u SET u.status = ? WHERE u.name = ?", nativeQuery = true)
|
||||
int updateUserSetStatusForNameNative(Integer status, String name);
|
||||
|
||||
@Query(value = "INSERT INTO Users (name, age, email, status) VALUES (:name, :age, :email, :status)", nativeQuery = true)
|
||||
@Modifying
|
||||
void insertUser(@Param("name") String name, @Param("age") Integer age, @Param("status") Integer status, @Param("email") String email);
|
||||
|
||||
@Modifying
|
||||
@Query(value = "UPDATE Users u SET status = ? WHERE u.name = ?", nativeQuery = true)
|
||||
int updateUserSetStatusForNameNativePostgres(Integer status, String name);
|
||||
|
||||
@Query(value = "SELECT u FROM User u WHERE u.name IN :names")
|
||||
List<User> findUserByNameList(@Param("names") Collection<String> names);
|
||||
}
|
||||
-10
@@ -1,10 +0,0 @@
|
||||
package com.baeldung.boot.user;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import com.baeldung.boot.domain.User;
|
||||
|
||||
public interface UserRepositoryCustom {
|
||||
List<User> findUserByEmails(Set<String> emails);
|
||||
}
|
||||
-43
@@ -1,43 +0,0 @@
|
||||
package com.baeldung.boot.user;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Path;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import com.baeldung.boot.domain.User;
|
||||
|
||||
public class UserRepositoryCustomImpl implements UserRepositoryCustom {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Override
|
||||
public List<User> findUserByEmails(Set<String> emails) {
|
||||
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<User> query = cb.createQuery(User.class);
|
||||
Root<User> user = query.from(User.class);
|
||||
|
||||
Path<String> emailPath = user.get("email");
|
||||
|
||||
List<Predicate> predicates = new ArrayList<>();
|
||||
for (String email : emails) {
|
||||
|
||||
predicates.add(cb.like(emailPath, email));
|
||||
|
||||
}
|
||||
query.select(user)
|
||||
.where(cb.or(predicates.toArray(new Predicate[predicates.size()])));
|
||||
|
||||
return entityManager.createQuery(query)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
}
|
||||
+3
-3
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.batchinserts;
|
||||
package com.baeldung.boot.web.controllers;
|
||||
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Arrays;
|
||||
@@ -9,8 +9,8 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.batchinserts.model.Customer;
|
||||
import com.baeldung.batchinserts.repository.CustomerRepository;
|
||||
import com.baeldung.boot.daos.CustomerRepository;
|
||||
import com.baeldung.boot.domain.Customer;
|
||||
|
||||
/**
|
||||
* A simple controller to test the JPA CrudRepository operations
|
||||
@@ -1,130 +0,0 @@
|
||||
package com.baeldung.domain.user;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private int id;
|
||||
private String name;
|
||||
private LocalDate creationDate;
|
||||
private LocalDate lastLoginDate;
|
||||
private boolean active;
|
||||
private int age;
|
||||
@Column(unique = true, nullable = false)
|
||||
private String email;
|
||||
private Integer status;
|
||||
@OneToMany
|
||||
List<Possession> possessionList;
|
||||
|
||||
public User() {
|
||||
super();
|
||||
}
|
||||
|
||||
public User(String name, LocalDate creationDate, String email, Integer status) {
|
||||
this.name = name;
|
||||
this.creationDate = creationDate;
|
||||
this.email = email;
|
||||
this.status = status;
|
||||
this.active = true;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public LocalDate getCreationDate() {
|
||||
return creationDate;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(final String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public Integer getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(Integer status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(final int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public List<Possession> getPossessionList() {
|
||||
return possessionList;
|
||||
}
|
||||
|
||||
public void setPossessionList(List<Possession> possessionList) {
|
||||
this.possessionList = possessionList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append("User [name=").append(name).append(", id=").append(id).append("]");
|
||||
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;
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -17,9 +17,9 @@ import org.springframework.transaction.PlatformTransactionManager;
|
||||
import javax.sql.DataSource;
|
||||
import java.util.HashMap;
|
||||
|
||||
//@Configuration
|
||||
@Configuration
|
||||
@PropertySource({"classpath:persistence-multiple-db.properties"})
|
||||
@EnableJpaRepositories(basePackages = "com.baeldung.multipledb", entityManagerFactoryRef = "productEntityManager", transactionManagerRef = "productTransactionManager")
|
||||
@EnableJpaRepositories(basePackages = "com.baeldung.multipledb.dao.product", entityManagerFactoryRef = "productEntityManager", transactionManagerRef = "productTransactionManager")
|
||||
@Profile("!tc")
|
||||
public class PersistenceProductConfiguration {
|
||||
@Autowired
|
||||
@@ -35,7 +35,7 @@ public class PersistenceProductConfiguration {
|
||||
public LocalContainerEntityManagerFactoryBean productEntityManager() {
|
||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
em.setDataSource(productDataSource());
|
||||
em.setPackagesToScan("com.baeldung.multipledb");
|
||||
em.setPackagesToScan("com.baeldung.multipledb.model.product");
|
||||
|
||||
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||
em.setJpaVendorAdapter(vendorAdapter);
|
||||
|
||||
+4
-3
@@ -14,9 +14,9 @@ import org.springframework.transaction.PlatformTransactionManager;
|
||||
import javax.sql.DataSource;
|
||||
import java.util.HashMap;
|
||||
|
||||
//@Configuration
|
||||
@Configuration
|
||||
@PropertySource({"classpath:persistence-multiple-db.properties"})
|
||||
@EnableJpaRepositories(basePackages = "com.baeldung.multipledb", entityManagerFactoryRef = "userEntityManager", transactionManagerRef = "userTransactionManager")
|
||||
@EnableJpaRepositories(basePackages = "com.baeldung.multipledb.dao.user", entityManagerFactoryRef = "userEntityManager", transactionManagerRef = "userTransactionManager")
|
||||
@Profile("!tc")
|
||||
public class PersistenceUserConfiguration {
|
||||
@Autowired
|
||||
@@ -31,9 +31,10 @@ public class PersistenceUserConfiguration {
|
||||
@Primary
|
||||
@Bean
|
||||
public LocalContainerEntityManagerFactoryBean userEntityManager() {
|
||||
System.out.println("loading config");
|
||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
em.setDataSource(userDataSource());
|
||||
em.setPackagesToScan("com.baeldung.multipledb");
|
||||
em.setPackagesToScan("com.baeldung.multipledb.model.user");
|
||||
|
||||
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||
em.setJpaVendorAdapter(vendorAdapter);
|
||||
|
||||
-11
@@ -1,11 +0,0 @@
|
||||
package com.baeldung.multipledb;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
|
||||
public interface ProductRepository extends PagingAndSortingRepository<Product, Integer> {
|
||||
|
||||
List<Product> findAllByPrice(double price, Pageable pageable);
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.multipledb.dao.product;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
|
||||
import com.baeldung.multipledb.model.product.ProductMultipleDB;
|
||||
|
||||
public interface ProductRepository extends PagingAndSortingRepository<ProductMultipleDB, Integer> {
|
||||
|
||||
List<ProductMultipleDB> findAllByPrice(double price, Pageable pageable);
|
||||
}
|
||||
+3
-3
@@ -1,9 +1,9 @@
|
||||
package com.baeldung.multipledb;
|
||||
package com.baeldung.multipledb.dao.user;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import com.baeldung.boot.domain.Possession;
|
||||
import com.baeldung.multipledb.model.user.PossessionMultipleDB;
|
||||
|
||||
public interface PossessionRepository extends JpaRepository<Possession, Long> {
|
||||
public interface PossessionRepository extends JpaRepository<PossessionMultipleDB, Long> {
|
||||
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.multipledb.dao.user;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import com.baeldung.multipledb.model.user.UserMultipleDB;
|
||||
|
||||
public interface UserRepository extends JpaRepository<UserMultipleDB, Integer> {
|
||||
}
|
||||
+6
-6
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.multipledb;
|
||||
package com.baeldung.multipledb.model.product;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
@@ -6,7 +6,7 @@ import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(schema = "products")
|
||||
public class Product {
|
||||
public class ProductMultipleDB {
|
||||
|
||||
@Id
|
||||
private int id;
|
||||
@@ -15,19 +15,19 @@ public class Product {
|
||||
|
||||
private double price;
|
||||
|
||||
public Product() {
|
||||
public ProductMultipleDB() {
|
||||
super();
|
||||
}
|
||||
|
||||
private Product(int id, String name, double price) {
|
||||
private ProductMultipleDB(int id, String name, double price) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public static Product from(int id, String name, double price) {
|
||||
return new Product(id, name, price);
|
||||
public static ProductMultipleDB from(int id, String name, double price) {
|
||||
return new ProductMultipleDB(id, name, price);
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
+5
-5
@@ -1,10 +1,10 @@
|
||||
package com.baeldung.multipledb;
|
||||
package com.baeldung.multipledb.model.user;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table
|
||||
public class Possession {
|
||||
public class PossessionMultipleDB {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@@ -12,11 +12,11 @@ public class Possession {
|
||||
|
||||
private String name;
|
||||
|
||||
public Possession() {
|
||||
public PossessionMultipleDB() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Possession(final String name) {
|
||||
public PossessionMultipleDB(final String name) {
|
||||
super();
|
||||
|
||||
this.name = name;
|
||||
@@ -58,7 +58,7 @@ public class Possession {
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
final Possession other = (Possession) obj;
|
||||
final PossessionMultipleDB other = (PossessionMultipleDB) obj;
|
||||
if (id != other.id) {
|
||||
return false;
|
||||
}
|
||||
+16
-4
@@ -1,11 +1,12 @@
|
||||
package com.baeldung.multipledb;
|
||||
package com.baeldung.multipledb.model.user;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
public class UserMultipleDB {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@@ -15,12 +16,15 @@ public class User {
|
||||
@Column(unique = true, nullable = false)
|
||||
private String email;
|
||||
private Integer status;
|
||||
|
||||
@OneToMany
|
||||
List<PossessionMultipleDB> possessionList;
|
||||
|
||||
public User() {
|
||||
public UserMultipleDB() {
|
||||
super();
|
||||
}
|
||||
|
||||
public User(String name, String email, Integer status) {
|
||||
public UserMultipleDB(String name, String email, Integer status) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
this.status = status;
|
||||
@@ -66,6 +70,14 @@ public class User {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public List<PossessionMultipleDB> getPossessionList() {
|
||||
return possessionList;
|
||||
}
|
||||
|
||||
public void setPossessionList(List<PossessionMultipleDB> possessionList) {
|
||||
this.possessionList = possessionList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
@@ -1,9 +1,5 @@
|
||||
spring.main.allow-bean-definition-overriding=true
|
||||
|
||||
# envers.X
|
||||
#spring.jpa.properties.hibernate.hbm2ddl.import_files=import_entities.sql
|
||||
spring.datasource.data=classpath:import_entities.sql
|
||||
|
||||
spring.jpa.properties.hibernate.jdbc.batch_size=4
|
||||
spring.jpa.properties.hibernate.order_inserts=true
|
||||
spring.jpa.properties.hibernate.order_updates=true
|
||||
|
||||
+4
-8
@@ -9,21 +9,17 @@ import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import com.baeldung.batchinserts.CustomerController;
|
||||
import com.baeldung.batchinserts.repository.CustomerRepository;
|
||||
import com.baeldung.config.PersistenceConfiguration;
|
||||
import com.baeldung.config.PersistenceProductConfiguration;
|
||||
import com.baeldung.config.PersistenceUserConfiguration;
|
||||
import com.baeldung.boot.Application;
|
||||
import com.baeldung.boot.daos.CustomerRepository;
|
||||
import com.baeldung.boot.web.controllers.CustomerController;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@SpringBootTest(classes=Application.class)
|
||||
@AutoConfigureMockMvc
|
||||
@ContextConfiguration(classes = { PersistenceConfiguration.class, PersistenceProductConfiguration.class, PersistenceUserConfiguration.class })
|
||||
public class BatchInsertIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
|
||||
+172
-32
@@ -1,8 +1,5 @@
|
||||
package com.baeldung.boot.daos;
|
||||
|
||||
import com.baeldung.boot.domain.User;
|
||||
import com.baeldung.boot.user.UserRepository;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -13,10 +10,18 @@ import org.springframework.data.jpa.domain.JpaSort;
|
||||
import org.springframework.data.mapping.PropertyReferenceException;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.baeldung.boot.daos.user.UserRepository;
|
||||
import com.baeldung.boot.domain.User;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.Query;
|
||||
import java.time.LocalDate;
|
||||
import java.util.*;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
class UserRepositoryCommon {
|
||||
|
||||
@@ -33,6 +38,8 @@ class UserRepositoryCommon {
|
||||
|
||||
@Autowired
|
||||
protected UserRepository userRepository;
|
||||
@Autowired
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
@@ -255,9 +262,9 @@ class UserRepositoryCommon {
|
||||
|
||||
@Test
|
||||
public void givenUsersInDB_WhenFindAllWithSortByName_ThenReturnUsersSorted() {
|
||||
userRepository.save(new User(USER_NAME_ADAM, USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, INACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_ADAM, LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, INACTIVE_STATUS));
|
||||
|
||||
List<User> usersSortByName = userRepository.findAll(new Sort(Sort.Direction.ASC, "name"));
|
||||
|
||||
@@ -267,9 +274,9 @@ class UserRepositoryCommon {
|
||||
|
||||
@Test(expected = PropertyReferenceException.class)
|
||||
public void givenUsersInDB_WhenFindAllSortWithFunction_ThenThrowException() {
|
||||
userRepository.save(new User(USER_NAME_ADAM, USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, INACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_ADAM, LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, INACTIVE_STATUS));
|
||||
|
||||
userRepository.findAll(new Sort(Sort.Direction.ASC, "name"));
|
||||
|
||||
@@ -281,9 +288,9 @@ class UserRepositoryCommon {
|
||||
|
||||
@Test
|
||||
public void givenUsersInDB_WhenFindAllSortWithFunctionQueryAnnotationJPQL_ThenReturnUsersSorted() {
|
||||
userRepository.save(new User(USER_NAME_ADAM, USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, INACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_ADAM, LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, INACTIVE_STATUS));
|
||||
|
||||
userRepository.findAllUsers(new Sort("name"));
|
||||
|
||||
@@ -295,12 +302,12 @@ class UserRepositoryCommon {
|
||||
|
||||
@Test
|
||||
public void givenUsersInDB_WhenFindAllWithPageRequestQueryAnnotationJPQL_ThenReturnPageOfUsers() {
|
||||
userRepository.save(new User(USER_NAME_ADAM, USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", USER_EMAIL4, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE2", USER_EMAIL5, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", USER_EMAIL6, INACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_ADAM, LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", LocalDate.now(), USER_EMAIL4, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE2", LocalDate.now(), USER_EMAIL5, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", LocalDate.now(), USER_EMAIL6, INACTIVE_STATUS));
|
||||
|
||||
Page<User> usersPage = userRepository.findAllUsersWithPagination(new PageRequest(1, 3));
|
||||
|
||||
@@ -311,27 +318,27 @@ class UserRepositoryCommon {
|
||||
|
||||
@Test
|
||||
public void givenUsersInDB_WhenFindAllWithPageRequestQueryAnnotationNative_ThenReturnPageOfUsers() {
|
||||
userRepository.save(new User(USER_NAME_ADAM, USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", USER_EMAIL4, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE2", USER_EMAIL5, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", USER_EMAIL6, INACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_ADAM, LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", LocalDate.now(), USER_EMAIL4, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE2", LocalDate.now(), USER_EMAIL5, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", LocalDate.now(), USER_EMAIL6, INACTIVE_STATUS));
|
||||
|
||||
Page<User> usersSortByNameLength = userRepository.findAllUsersWithPaginationNative(new PageRequest(1, 3));
|
||||
|
||||
assertThat(usersSortByNameLength.getContent()
|
||||
.get(0)
|
||||
.getName()).isEqualTo("SAMPLE1");
|
||||
.getName()).isEqualTo(USER_NAME_PETER);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenUsersInDB_WhenUpdateStatusForNameModifyingQueryAnnotationJPQL_ThenModifyMatchingUsers() {
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", USER_EMAIL4, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", LocalDate.now(), USER_EMAIL4, ACTIVE_STATUS));
|
||||
|
||||
int updatedUsersSize = userRepository.updateUserSetStatusForName(INACTIVE_STATUS, "SAMPLE");
|
||||
|
||||
@@ -386,8 +393,8 @@ class UserRepositoryCommon {
|
||||
@Test
|
||||
@Transactional
|
||||
public void whenInsertedWithQuery_ThenUserIsPersisted() {
|
||||
userRepository.insertUser(USER_NAME_ADAM, 1, ACTIVE_STATUS, USER_EMAIL);
|
||||
userRepository.insertUser(USER_NAME_PETER, 1, ACTIVE_STATUS, USER_EMAIL2);
|
||||
userRepository.insertUser(USER_NAME_ADAM, 1, USER_EMAIL, ACTIVE_STATUS, true);
|
||||
userRepository.insertUser(USER_NAME_PETER, 1, USER_EMAIL2, ACTIVE_STATUS, true);
|
||||
|
||||
User userAdam = userRepository.findUserByNameLike(USER_NAME_ADAM);
|
||||
User userPeter = userRepository.findUserByNameLike(USER_NAME_PETER);
|
||||
@@ -397,7 +404,140 @@ class UserRepositoryCommon {
|
||||
assertThat(userPeter).isNotNull();
|
||||
assertThat(userPeter.getEmail()).isEqualTo(USER_EMAIL2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenTwoUsers_whenFindByNameUsr01_ThenUserUsr01() {
|
||||
User usr01 = new User("usr01", LocalDate.now(), "[email protected]", 1);
|
||||
User usr02 = new User("usr02", LocalDate.now(), "[email protected]", 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(), "[email protected]", 1);
|
||||
User usr02 = new User("usr02", LocalDate.now(), "[email protected]", 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(), "[email protected]", 1);
|
||||
User usr02 = new User("usr02", LocalDate.now(), "[email protected]", 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), "[email protected]", 1);
|
||||
User usr02 = new User("usr02", LocalDate.of(2018, 6, 1), "[email protected]", 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), "[email protected]", 1);
|
||||
User usr02 = new User("usr02", LocalDate.of(2018, 6, 1), "[email protected]", 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), "[email protected]", 1);
|
||||
usr01.setLastLoginDate(LocalDate.now());
|
||||
User usr02 = new User("usr02", LocalDate.of(2018, 6, 1), "[email protected]", 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), "[email protected]", 1);
|
||||
usr01.setLastLoginDate(LocalDate.now());
|
||||
User usr02 = new User("usr02", LocalDate.of(2018, 6, 1), "[email protected]", 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), "[email protected]", 1);
|
||||
usr01.setLastLoginDate(LocalDate.now());
|
||||
User usr02 = new User("usr02", LocalDate.of(2018, 6, 1), "[email protected]", 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
|
||||
public void cleanUp() {
|
||||
userRepository.deleteAll();
|
||||
|
||||
+6
-6
@@ -1,9 +1,7 @@
|
||||
package com.baeldung.boot.daos;
|
||||
|
||||
import com.baeldung.boot.Application;
|
||||
import com.baeldung.boot.config.PersistenceConfiguration;
|
||||
import com.baeldung.boot.domain.User;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
@@ -11,6 +9,8 @@ import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
@@ -24,10 +24,10 @@ public class UserRepositoryIntegrationTest extends UserRepositoryCommon {
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenUsersInDBWhenUpdateStatusForNameModifyingQueryAnnotationNativeThenModifyMatchingUsers() {
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", USER_EMAIL4, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", LocalDate.now(), USER_EMAIL4, ACTIVE_STATUS));
|
||||
userRepository.flush();
|
||||
|
||||
int updatedUsersSize = userRepository.updateUserSetStatusForNameNative(INACTIVE_STATUS, "SAMPLE");
|
||||
|
||||
+8
-5
@@ -1,5 +1,6 @@
|
||||
package com.baeldung.boot.daos;
|
||||
|
||||
import com.baeldung.boot.Application;
|
||||
import com.baeldung.boot.domain.User;
|
||||
import com.baeldung.util.BaeldungPostgresqlContainer;
|
||||
import org.junit.ClassRule;
|
||||
@@ -11,13 +12,15 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.testcontainers.containers.PostgreSQLContainer;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Created by adam.
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@SpringBootTest(classes = Application.class)
|
||||
@ActiveProfiles({"tc", "tc-auto"})
|
||||
public class UserRepositoryTCAutoIntegrationTest extends UserRepositoryCommon {
|
||||
|
||||
@@ -27,10 +30,10 @@ public class UserRepositoryTCAutoIntegrationTest extends UserRepositoryCommon {
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenUsersInDB_WhenUpdateStatusForNameModifyingQueryAnnotationNativePostgres_ThenModifyMatchingUsers() {
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", USER_EMAIL4, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", LocalDate.now(), USER_EMAIL4, ACTIVE_STATUS));
|
||||
userRepository.flush();
|
||||
|
||||
int updatedUsersSize = userRepository.updateUserSetStatusForNameNativePostgres(INACTIVE_STATUS, "SAMPLE");
|
||||
|
||||
+8
-6
@@ -1,5 +1,7 @@
|
||||
package com.baeldung.boot.daos;
|
||||
|
||||
import com.baeldung.boot.Application;
|
||||
import com.baeldung.boot.domain.User;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -13,12 +15,12 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.testcontainers.containers.PostgreSQLContainer;
|
||||
|
||||
import com.baeldung.boot.domain.User;
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@SpringBootTest(classes = Application.class)
|
||||
@ActiveProfiles("tc")
|
||||
@ContextConfiguration(initializers = {UserRepositoryTCIntegrationTest.Initializer.class})
|
||||
public class UserRepositoryTCIntegrationTest extends UserRepositoryCommon {
|
||||
@@ -32,10 +34,10 @@ public class UserRepositoryTCIntegrationTest extends UserRepositoryCommon {
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenUsersInDB_WhenUpdateStatusForNameModifyingQueryAnnotationNative_ThenModifyMatchingUsers() {
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", USER_EMAIL4, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", LocalDate.now(), USER_EMAIL4, ACTIVE_STATUS));
|
||||
userRepository.flush();
|
||||
|
||||
int updatedUsersSize = userRepository.updateUserSetStatusForNameNativePostgres(INACTIVE_STATUS, "SAMPLE");
|
||||
|
||||
-544
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user