Merge pull request #6871 from eugenp/fix-spring-data-jpa
Fix spring data jpa
This commit is contained in:
commit
b3c89bbf13
|
@ -90,4 +90,8 @@
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<start-class>com.baeldung.boot.Application</start-class>
|
||||||
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -1,10 +1,11 @@
|
||||||
package com.baeldung;
|
package com.baeldung.boot;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
|
|
||||||
import com.baeldung.dao.repositories.impl.ExtendedRepositoryImpl;
|
import com.baeldung.boot.daos.impl.ExtendedRepositoryImpl;
|
||||||
|
import com.baeldung.multipledb.MultipleDbApplication;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableJpaRepositories(repositoryBaseClass = ExtendedRepositoryImpl.class)
|
@EnableJpaRepositories(repositoryBaseClass = ExtendedRepositoryImpl.class)
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.baeldung.boot.config;
|
||||||
|
|
||||||
|
import com.baeldung.boot.services.IBarService;
|
||||||
|
import com.baeldung.boot.services.impl.BarSpringDataJpaService;
|
||||||
|
import org.springframework.context.annotation.*;
|
||||||
|
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
||||||
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@Profile("!tc")
|
||||||
|
@EnableTransactionManagement
|
||||||
|
@EnableJpaAuditing
|
||||||
|
public class PersistenceConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public IBarService barSpringDataJpaService() {
|
||||||
|
return new BarSpringDataJpaService();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,10 +1,11 @@
|
||||||
package com.baeldung.dao.repositories;
|
package com.baeldung.boot.daos;
|
||||||
|
|
||||||
import com.baeldung.domain.Article;
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
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 com.baeldung.boot.domain.Article;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package com.baeldung.dao.repositories;
|
package com.baeldung.boot.daos;
|
||||||
|
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import com.baeldung.domain.Item;
|
import com.baeldung.boot.domain.Item;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface CustomItemRepository {
|
public interface CustomItemRepository {
|
|
@ -1,8 +1,8 @@
|
||||||
package com.baeldung.dao.repositories;
|
package com.baeldung.boot.daos;
|
||||||
|
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import com.baeldung.domain.ItemType;
|
import com.baeldung.boot.domain.ItemType;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface CustomItemTypeRepository {
|
public interface CustomItemTypeRepository {
|
|
@ -1,8 +1,8 @@
|
||||||
package com.baeldung.batchinserts.repository;
|
package com.baeldung.boot.daos;
|
||||||
|
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
import com.baeldung.batchinserts.model.Customer;
|
import com.baeldung.boot.domain.Customer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JPA CrudRepository interface
|
* JPA CrudRepository interface
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.dao.repositories;
|
package com.baeldung.boot.daos;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.List;
|
import java.util.List;
|
|
@ -1,6 +1,6 @@
|
||||||
package com.baeldung.dao.repositories;
|
package com.baeldung.boot.daos;
|
||||||
|
|
||||||
import com.baeldung.domain.Student;
|
import com.baeldung.boot.domain.Student;
|
||||||
|
|
||||||
public interface ExtendedStudentRepository extends ExtendedRepository<Student, Long> {
|
public interface ExtendedStudentRepository extends ExtendedRepository<Student, Long> {
|
||||||
}
|
}
|
|
@ -1,8 +1,9 @@
|
||||||
package com.baeldung.dao.repositories;
|
package com.baeldung.boot.daos;
|
||||||
|
|
||||||
import com.baeldung.domain.Bar;
|
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
|
import com.baeldung.boot.domain.Bar;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
public interface IBarCrudRepository extends CrudRepository<Bar, Serializable> {
|
public interface IBarCrudRepository extends CrudRepository<Bar, Serializable> {
|
|
@ -1,10 +1,11 @@
|
||||||
package com.baeldung.dao;
|
package com.baeldung.boot.daos;
|
||||||
|
|
||||||
import com.baeldung.domain.Foo;
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
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 com.baeldung.boot.domain.Foo;
|
||||||
|
|
||||||
public interface IFooDao extends JpaRepository<Foo, Long> {
|
public interface IFooDao extends JpaRepository<Foo, Long> {
|
||||||
|
|
||||||
@Query("SELECT f FROM Foo f WHERE LOWER(f.name) = LOWER(:name)")
|
@Query("SELECT f FROM Foo f WHERE LOWER(f.name) = LOWER(:name)")
|
|
@ -1,7 +1,8 @@
|
||||||
package com.baeldung.dao.repositories;
|
package com.baeldung.boot.daos;
|
||||||
|
|
||||||
import com.baeldung.domain.MerchandiseEntity;
|
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
|
import com.baeldung.boot.domain.MerchandiseEntity;
|
||||||
|
|
||||||
public interface InventoryRepository extends CrudRepository<MerchandiseEntity, Long> {
|
public interface InventoryRepository extends CrudRepository<MerchandiseEntity, Long> {
|
||||||
}
|
}
|
|
@ -1,9 +1,9 @@
|
||||||
package com.baeldung.dao.repositories;
|
package com.baeldung.boot.daos;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import com.baeldung.domain.ItemType;
|
import com.baeldung.boot.domain.ItemType;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface ItemTypeRepository extends JpaRepository<ItemType, Long>, CustomItemTypeRepository, CustomItemRepository {
|
public interface ItemTypeRepository extends JpaRepository<ItemType, Long>, CustomItemTypeRepository, CustomItemRepository {
|
|
@ -1,9 +1,9 @@
|
||||||
package com.baeldung.dao.repositories;
|
package com.baeldung.boot.daos;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import com.baeldung.domain.Location;
|
import com.baeldung.boot.domain.Location;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface LocationRepository extends JpaRepository<Location, Long> {
|
public interface LocationRepository extends JpaRepository<Location, Long> {
|
|
@ -1,10 +1,10 @@
|
||||||
package com.baeldung.dao.repositories;
|
package com.baeldung.boot.daos;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.springframework.data.repository.Repository;
|
import org.springframework.data.repository.Repository;
|
||||||
|
|
||||||
import com.baeldung.domain.Location;
|
import com.baeldung.boot.domain.Location;
|
||||||
|
|
||||||
@org.springframework.stereotype.Repository
|
@org.springframework.stereotype.Repository
|
||||||
public interface ReadOnlyLocationRepository extends Repository<Location, Long> {
|
public interface ReadOnlyLocationRepository extends Repository<Location, Long> {
|
|
@ -1,11 +1,11 @@
|
||||||
package com.baeldung.dao.repositories;
|
package com.baeldung.boot.daos;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import com.baeldung.domain.Store;
|
import com.baeldung.boot.domain.Store;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface StoreRepository extends JpaRepository<Store, Long> {
|
public interface StoreRepository extends JpaRepository<Store, Long> {
|
|
@ -1,12 +1,12 @@
|
||||||
package com.baeldung.dao.repositories.impl;
|
package com.baeldung.boot.daos.impl;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import com.baeldung.dao.repositories.CustomItemRepository;
|
import com.baeldung.boot.daos.CustomItemRepository;
|
||||||
import com.baeldung.domain.Item;
|
import com.baeldung.boot.domain.Item;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public class CustomItemRepositoryImpl implements CustomItemRepository {
|
public class CustomItemRepositoryImpl implements CustomItemRepository {
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.dao.repositories.impl;
|
package com.baeldung.boot.daos.impl;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
|
|
||||||
|
@ -7,8 +7,8 @@ import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import com.baeldung.domain.ItemType;
|
import com.baeldung.boot.daos.CustomItemTypeRepository;
|
||||||
import com.baeldung.dao.repositories.CustomItemTypeRepository;
|
import com.baeldung.boot.domain.ItemType;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public class CustomItemTypeRepositoryImpl implements CustomItemTypeRepository {
|
public class CustomItemTypeRepositoryImpl implements CustomItemTypeRepository {
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.dao.repositories.impl;
|
package com.baeldung.boot.daos.impl;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -10,10 +10,11 @@ import javax.persistence.criteria.CriteriaQuery;
|
||||||
import javax.persistence.criteria.Root;
|
import javax.persistence.criteria.Root;
|
||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
||||||
|
|
||||||
import com.baeldung.dao.repositories.ExtendedRepository;
|
|
||||||
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
|
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
|
||||||
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
|
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
|
||||||
|
|
||||||
|
import com.baeldung.boot.daos.ExtendedRepository;
|
||||||
|
|
||||||
public class ExtendedRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements ExtendedRepository<T, ID> {
|
public class ExtendedRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements ExtendedRepository<T, ID> {
|
||||||
|
|
||||||
private EntityManager entityManager;
|
private EntityManager entityManager;
|
|
@ -1,8 +1,9 @@
|
||||||
package com.baeldung.dao.repositories.impl;
|
package com.baeldung.boot.daos.impl;
|
||||||
|
|
||||||
import com.baeldung.domain.Person;
|
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import com.baeldung.boot.domain.Person;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.PersistenceContext;
|
import javax.persistence.PersistenceContext;
|
||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
|
@ -1,8 +1,9 @@
|
||||||
package com.baeldung.dao.repositories.user;
|
package com.baeldung.boot.daos.user;
|
||||||
|
|
||||||
import com.baeldung.domain.user.Possession;
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
import com.baeldung.boot.domain.Possession;
|
||||||
|
|
||||||
public interface PossessionRepository extends JpaRepository<Possession, Long> {
|
public interface PossessionRepository extends JpaRepository<Possession, Long> {
|
||||||
|
|
||||||
}
|
}
|
|
@ -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.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.data.domain.Sort;
|
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.jpa.repository.Query;
|
||||||
import org.springframework.data.repository.query.Param;
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
|
import com.baeldung.boot.domain.User;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -18,12 +19,12 @@ 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();
|
||||||
|
|
||||||
|
@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)
|
@Query(value = "SELECT * FROM Users u WHERE u.status = 1", nativeQuery = true)
|
||||||
Collection<User> findAllActiveUsersNative();
|
Collection<User> findAllActiveUsersNative();
|
||||||
|
|
||||||
|
@ -80,7 +81,7 @@ public interface UserRepository extends JpaRepository<User, Integer> , UserRepos
|
||||||
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);
|
void deleteAllByCreationDateAfter(LocalDate date);
|
||||||
|
|
||||||
|
@ -89,11 +90,10 @@ public interface UserRepository extends JpaRepository<User, Integer> , UserRepos
|
||||||
void deactivateUsersNotLoggedInSince(@Param("date") LocalDate date);
|
void deactivateUsersNotLoggedInSince(@Param("date") LocalDate date);
|
||||||
|
|
||||||
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||||
@Query("delete from User u where u.active = false")
|
@Query("delete User u where u.active = false")
|
||||||
int deleteDeactivatedUsers();
|
int deleteDeactivatedUsers();
|
||||||
|
|
||||||
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
@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();
|
void addDeletedColumn();
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,11 +1,11 @@
|
||||||
package com.baeldung.dao.repositories.user;
|
package com.baeldung.boot.daos.user;
|
||||||
|
|
||||||
import java.util.Collection;
|
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 java.util.function.Predicate;
|
||||||
|
|
||||||
import com.baeldung.domain.user.User;
|
import com.baeldung.boot.domain.User;
|
||||||
|
|
||||||
public interface UserRepositoryCustom {
|
public interface UserRepositoryCustom {
|
||||||
List<User> findUserByEmails(Set<String> emails);
|
List<User> findUserByEmails(Set<String> emails);
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.dao.repositories.user;
|
package com.baeldung.boot.daos.user;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
@ -15,7 +15,7 @@ import javax.persistence.criteria.Path;
|
||||||
import javax.persistence.criteria.Predicate;
|
import javax.persistence.criteria.Predicate;
|
||||||
import javax.persistence.criteria.Root;
|
import javax.persistence.criteria.Root;
|
||||||
|
|
||||||
import com.baeldung.domain.user.User;
|
import com.baeldung.boot.domain.User;
|
||||||
|
|
||||||
public class UserRepositoryCustomImpl implements UserRepositoryCustom {
|
public class UserRepositoryCustomImpl implements UserRepositoryCustom {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package com.baeldung.ddd.event;
|
package com.baeldung.boot.ddd.event;
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
|
@ -1,7 +1,7 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package com.baeldung.ddd.event;
|
package com.baeldung.boot.ddd.event;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
|
@ -1,7 +1,7 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package com.baeldung.ddd.event;
|
package com.baeldung.boot.ddd.event;
|
||||||
|
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package com.baeldung.ddd.event;
|
package com.baeldung.boot.ddd.event;
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
|
@ -1,7 +1,7 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package com.baeldung.ddd.event;
|
package com.baeldung.boot.ddd.event;
|
||||||
|
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package com.baeldung.ddd.event;
|
package com.baeldung.boot.ddd.event;
|
||||||
|
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package com.baeldung.ddd.event;
|
package com.baeldung.boot.ddd.event;
|
||||||
|
|
||||||
import org.springframework.boot.SpringBootConfiguration;
|
import org.springframework.boot.SpringBootConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
@ -0,0 +1,8 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package com.baeldung.boot.ddd.event;
|
||||||
|
|
||||||
|
class DomainEvent {
|
||||||
|
|
||||||
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package com.baeldung.ddd.event;
|
package com.baeldung.boot.ddd.event;
|
||||||
|
|
||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.domain;
|
package com.baeldung.boot.domain;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.domain;
|
package com.baeldung.boot.domain;
|
||||||
|
|
||||||
import com.google.common.collect.Sets;
|
import com.google.common.collect.Sets;
|
||||||
import org.hibernate.annotations.OrderBy;
|
import org.hibernate.annotations.OrderBy;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.batchinserts.model;
|
package com.baeldung.boot.domain;
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.domain;
|
package com.baeldung.boot.domain;
|
||||||
|
|
||||||
import org.hibernate.envers.Audited;
|
import org.hibernate.envers.Audited;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.domain;
|
package com.baeldung.boot.domain;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.domain;
|
package com.baeldung.boot.domain;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.domain;
|
package com.baeldung.boot.domain;
|
||||||
|
|
||||||
import javax.persistence.Embeddable;
|
import javax.persistence.Embeddable;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.domain;
|
package com.baeldung.boot.domain;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.domain;
|
package com.baeldung.boot.domain;
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.domain;
|
package com.baeldung.boot.domain;
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
|
@ -1,6 +1,7 @@
|
||||||
package com.baeldung.domain.user;
|
package com.baeldung.boot.domain;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
|
import com.baeldung.boot.domain.Possession;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table
|
@Table
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.domain;
|
package com.baeldung.boot.domain;
|
||||||
|
|
||||||
import javax.persistence.Embeddable;
|
import javax.persistence.Embeddable;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.domain;
|
package com.baeldung.boot.domain;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.domain;
|
package com.baeldung.boot.domain;
|
||||||
|
|
||||||
import javax.persistence.ElementCollection;
|
import javax.persistence.ElementCollection;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
|
@ -1,6 +1,7 @@
|
||||||
package com.baeldung.domain.user;
|
package com.baeldung.boot.domain;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
@ -27,7 +28,7 @@ public class User {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
public User(String name, LocalDate creationDate, String email, Integer status) {
|
public User(String name, LocalDate creationDate,String email, Integer status) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.creationDate = creationDate;
|
this.creationDate = creationDate;
|
||||||
this.email = email;
|
this.email = email;
|
||||||
|
@ -51,10 +52,6 @@ public class User {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LocalDate getCreationDate() {
|
|
||||||
return creationDate;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getEmail() {
|
public String getEmail() {
|
||||||
return email;
|
return email;
|
||||||
}
|
}
|
||||||
|
@ -79,6 +76,10 @@ public class User {
|
||||||
this.age = age;
|
this.age = age;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public LocalDate getCreationDate() {
|
||||||
|
return creationDate;
|
||||||
|
}
|
||||||
|
|
||||||
public List<Possession> getPossessionList() {
|
public List<Possession> getPossessionList() {
|
||||||
return possessionList;
|
return possessionList;
|
||||||
}
|
}
|
||||||
|
@ -127,4 +128,5 @@ public class User {
|
||||||
public void setActive(boolean active) {
|
public void setActive(boolean active) {
|
||||||
this.active = active;
|
this.active = active;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.passenger;
|
package com.baeldung.boot.passenger;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.passenger;
|
package com.baeldung.boot.passenger;
|
||||||
|
|
||||||
import javax.persistence.Basic;
|
import javax.persistence.Basic;
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.passenger;
|
package com.baeldung.boot.passenger;
|
||||||
|
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.passenger;
|
package com.baeldung.boot.passenger;
|
||||||
|
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
package com.baeldung.boot.services;
|
||||||
|
|
||||||
|
import com.baeldung.boot.domain.Bar;
|
||||||
|
|
||||||
|
public interface IBarService extends IOperations<Bar> {
|
||||||
|
//
|
||||||
|
}
|
|
@ -1,9 +1,10 @@
|
||||||
package com.baeldung.services;
|
package com.baeldung.boot.services;
|
||||||
|
|
||||||
import com.baeldung.domain.Foo;
|
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
|
|
||||||
|
import com.baeldung.boot.domain.Foo;
|
||||||
|
|
||||||
public interface IFooService extends IOperations<Foo> {
|
public interface IFooService extends IOperations<Foo> {
|
||||||
|
|
||||||
Foo retrieveByName(String name);
|
Foo retrieveByName(String name);
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.services;
|
package com.baeldung.boot.services;
|
||||||
|
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package com.baeldung.services.impl;
|
package com.baeldung.boot.services.impl;
|
||||||
|
|
||||||
import com.baeldung.services.IOperations;
|
import com.baeldung.boot.services.IOperations;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
|
@ -1,6 +1,6 @@
|
||||||
package com.baeldung.services.impl;
|
package com.baeldung.boot.services.impl;
|
||||||
|
|
||||||
import com.baeldung.services.IOperations;
|
import com.baeldung.boot.services.IOperations;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
|
@ -1,8 +1,9 @@
|
||||||
package com.baeldung.services.impl;
|
package com.baeldung.boot.services.impl;
|
||||||
|
|
||||||
|
import com.baeldung.boot.daos.IBarCrudRepository;
|
||||||
|
import com.baeldung.boot.domain.Bar;
|
||||||
|
import com.baeldung.boot.services.IBarService;
|
||||||
|
|
||||||
import com.baeldung.domain.Bar;
|
|
||||||
import com.baeldung.dao.repositories.IBarCrudRepository;
|
|
||||||
import com.baeldung.services.IBarService;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
|
@ -1,9 +1,10 @@
|
||||||
package com.baeldung.services.impl;
|
package com.baeldung.boot.services.impl;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import com.baeldung.dao.IFooDao;
|
import com.baeldung.boot.daos.IFooDao;
|
||||||
import com.baeldung.domain.Foo;
|
import com.baeldung.boot.domain.Foo;
|
||||||
import com.baeldung.services.IFooService;
|
import com.baeldung.boot.services.IFooService;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.batchinserts;
|
package com.baeldung.boot.web.controllers;
|
||||||
|
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.util.Arrays;
|
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.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import com.baeldung.batchinserts.model.Customer;
|
import com.baeldung.boot.daos.CustomerRepository;
|
||||||
import com.baeldung.batchinserts.repository.CustomerRepository;
|
import com.baeldung.boot.domain.Customer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simple controller to test the JPA CrudRepository operations
|
* A simple controller to test the JPA CrudRepository operations
|
|
@ -1,97 +0,0 @@
|
||||||
package com.baeldung.config;
|
|
||||||
|
|
||||||
import com.baeldung.dao.repositories.impl.ExtendedRepositoryImpl;
|
|
||||||
import com.baeldung.services.IBarService;
|
|
||||||
import com.baeldung.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
|
|
||||||
@ComponentScan({ "com.baeldung.dao", "com.baeldung.services" })
|
|
||||||
@EnableTransactionManagement
|
|
||||||
@EnableJpaRepositories(basePackages = { "com.baeldung.dao" }, repositoryBaseClass = ExtendedRepositoryImpl.class)
|
|
||||||
@EnableJpaAuditing
|
|
||||||
@PropertySource("classpath:persistence.properties")
|
|
||||||
@Profile("!tc")
|
|
||||||
public class PersistenceConfiguration {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private Environment env;
|
|
||||||
|
|
||||||
public PersistenceConfiguration() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
|
||||||
final LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
|
|
||||||
emf.setDataSource(dataSource());
|
|
||||||
emf.setPackagesToScan("com.baeldung.domain");
|
|
||||||
|
|
||||||
final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
|
||||||
emf.setJpaVendorAdapter(vendorAdapter);
|
|
||||||
emf.setJpaProperties(hibernateProperties());
|
|
||||||
|
|
||||||
return emf;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public DataSource dataSource() {
|
|
||||||
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
|
||||||
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
|
|
||||||
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
|
|
||||||
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
|
|
||||||
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
|
|
||||||
|
|
||||||
return dataSource;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public PlatformTransactionManager transactionManager() {
|
|
||||||
final JpaTransactionManager transactionManager = new JpaTransactionManager();
|
|
||||||
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
|
|
||||||
return transactionManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
|
|
||||||
return new PersistenceExceptionTranslationPostProcessor();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public IBarService barSpringDataJpaService() {
|
|
||||||
return new BarSpringDataJpaService();
|
|
||||||
}
|
|
||||||
|
|
||||||
private final Properties hibernateProperties() {
|
|
||||||
final Properties hibernateProperties = new Properties();
|
|
||||||
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
|
||||||
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
|
||||||
|
|
||||||
hibernateProperties.setProperty("hibernate.show_sql", "true");
|
|
||||||
// hibernateProperties.setProperty("hibernate.format_sql", "true");
|
|
||||||
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
|
|
||||||
|
|
||||||
// Envers properties
|
|
||||||
hibernateProperties.setProperty("org.hibernate.envers.audit_table_suffix",
|
|
||||||
env.getProperty("envers.audit_table_suffix"));
|
|
||||||
|
|
||||||
return hibernateProperties;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,13 +0,0 @@
|
||||||
package com.baeldung.dao.repositories.product;
|
|
||||||
|
|
||||||
import com.baeldung.domain.product.Product;
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
|
@ -1,8 +0,0 @@
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
package com.baeldung.ddd.event;
|
|
||||||
|
|
||||||
class DomainEvent {
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.baeldung.multipledb;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class MultipleDbApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(MultipleDbApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.config;
|
package com.baeldung.multipledb;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -19,7 +19,7 @@ import java.util.HashMap;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@PropertySource({"classpath:persistence-multiple-db.properties"})
|
@PropertySource({"classpath:persistence-multiple-db.properties"})
|
||||||
@EnableJpaRepositories(basePackages = "com.baeldung.dao.repositories.product", entityManagerFactoryRef = "productEntityManager", transactionManagerRef = "productTransactionManager")
|
@EnableJpaRepositories(basePackages = "com.baeldung.multipledb.dao.product", entityManagerFactoryRef = "productEntityManager", transactionManagerRef = "productTransactionManager")
|
||||||
@Profile("!tc")
|
@Profile("!tc")
|
||||||
public class PersistenceProductConfiguration {
|
public class PersistenceProductConfiguration {
|
||||||
@Autowired
|
@Autowired
|
||||||
|
@ -35,7 +35,7 @@ public class PersistenceProductConfiguration {
|
||||||
public LocalContainerEntityManagerFactoryBean productEntityManager() {
|
public LocalContainerEntityManagerFactoryBean productEntityManager() {
|
||||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||||
em.setDataSource(productDataSource());
|
em.setDataSource(productDataSource());
|
||||||
em.setPackagesToScan("com.baeldung.domain.product");
|
em.setPackagesToScan("com.baeldung.multipledb.model.product");
|
||||||
|
|
||||||
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||||
em.setJpaVendorAdapter(vendorAdapter);
|
em.setJpaVendorAdapter(vendorAdapter);
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.config;
|
package com.baeldung.multipledb;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -16,7 +16,7 @@ import java.util.HashMap;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@PropertySource({"classpath:persistence-multiple-db.properties"})
|
@PropertySource({"classpath:persistence-multiple-db.properties"})
|
||||||
@EnableJpaRepositories(basePackages = "com.baeldung.dao.repositories.user", entityManagerFactoryRef = "userEntityManager", transactionManagerRef = "userTransactionManager")
|
@EnableJpaRepositories(basePackages = "com.baeldung.multipledb.dao.user", entityManagerFactoryRef = "userEntityManager", transactionManagerRef = "userTransactionManager")
|
||||||
@Profile("!tc")
|
@Profile("!tc")
|
||||||
public class PersistenceUserConfiguration {
|
public class PersistenceUserConfiguration {
|
||||||
@Autowired
|
@Autowired
|
||||||
|
@ -31,9 +31,10 @@ public class PersistenceUserConfiguration {
|
||||||
@Primary
|
@Primary
|
||||||
@Bean
|
@Bean
|
||||||
public LocalContainerEntityManagerFactoryBean userEntityManager() {
|
public LocalContainerEntityManagerFactoryBean userEntityManager() {
|
||||||
|
System.out.println("loading config");
|
||||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||||
em.setDataSource(userDataSource());
|
em.setDataSource(userDataSource());
|
||||||
em.setPackagesToScan("com.baeldung.domain.user");
|
em.setPackagesToScan("com.baeldung.multipledb.model.user");
|
||||||
|
|
||||||
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||||
em.setJpaVendorAdapter(vendorAdapter);
|
em.setJpaVendorAdapter(vendorAdapter);
|
|
@ -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);
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.baeldung.multipledb.dao.user;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
import com.baeldung.multipledb.model.user.PossessionMultipleDB;
|
||||||
|
|
||||||
|
public interface PossessionRepository extends JpaRepository<PossessionMultipleDB, Long> {
|
||||||
|
|
||||||
|
}
|
|
@ -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> {
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.domain.product;
|
package com.baeldung.multipledb.model.product;
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
@ -6,7 +6,7 @@ import javax.persistence.Table;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(schema = "products")
|
@Table(schema = "products")
|
||||||
public class Product {
|
public class ProductMultipleDB {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
private int id;
|
private int id;
|
||||||
|
@ -15,19 +15,19 @@ public class Product {
|
||||||
|
|
||||||
private double price;
|
private double price;
|
||||||
|
|
||||||
public Product() {
|
public ProductMultipleDB() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Product(int id, String name, double price) {
|
private ProductMultipleDB(int id, String name, double price) {
|
||||||
super();
|
super();
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.price = price;
|
this.price = price;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Product from(int id, String name, double price) {
|
public static ProductMultipleDB from(int id, String name, double price) {
|
||||||
return new Product(id, name, price);
|
return new ProductMultipleDB(id, name, price);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getId() {
|
public int getId() {
|
|
@ -0,0 +1,82 @@
|
||||||
|
package com.baeldung.multipledb.model.user;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table
|
||||||
|
public class PossessionMultipleDB {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public PossessionMultipleDB() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public PossessionMultipleDB(final String name) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
final int prime = 31;
|
||||||
|
int result = 1;
|
||||||
|
result = (prime * result) + (int) (id ^ (id >>> 32));
|
||||||
|
result = (prime * result) + ((name == null) ? 0 : name.hashCode());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (obj == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (getClass() != obj.getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
final PossessionMultipleDB other = (PossessionMultipleDB) obj;
|
||||||
|
if (id != other.id) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (name == null) {
|
||||||
|
if (other.name != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else if (!name.equals(other.name)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
final StringBuilder builder = new StringBuilder();
|
||||||
|
builder.append("Possesion [id=").append(id).append(", name=").append(name).append("]");
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,88 @@
|
||||||
|
package com.baeldung.multipledb.model.user;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "users")
|
||||||
|
public class UserMultipleDB {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private int id;
|
||||||
|
private String name;
|
||||||
|
private int age;
|
||||||
|
@Column(unique = true, nullable = false)
|
||||||
|
private String email;
|
||||||
|
private Integer status;
|
||||||
|
|
||||||
|
@OneToMany
|
||||||
|
List<PossessionMultipleDB> possessionList;
|
||||||
|
|
||||||
|
public UserMultipleDB() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserMultipleDB(String name, String email, Integer status) {
|
||||||
|
this.name = name;
|
||||||
|
this.email = email;
|
||||||
|
this.status = status;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 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<PossessionMultipleDB> getPossessionList() {
|
||||||
|
return possessionList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPossessionList(List<PossessionMultipleDB> 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,7 +0,0 @@
|
||||||
package com.baeldung.services;
|
|
||||||
|
|
||||||
import com.baeldung.domain.Bar;
|
|
||||||
|
|
||||||
public interface IBarService extends IOperations<Bar> {
|
|
||||||
//
|
|
||||||
}
|
|
|
@ -1,19 +1,3 @@
|
||||||
# spring.datasource.x
|
|
||||||
spring.datasource.driver-class-name=org.h2.Driver
|
|
||||||
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
|
|
||||||
spring.datasource.username=sa
|
|
||||||
spring.datasource.password=sa
|
|
||||||
|
|
||||||
# hibernate.X
|
|
||||||
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
|
||||||
hibernate.show_sql=true
|
|
||||||
hibernate.hbm2ddl.auto=create-drop
|
|
||||||
hibernate.cache.use_second_level_cache=true
|
|
||||||
hibernate.cache.use_query_cache=true
|
|
||||||
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
|
|
||||||
|
|
||||||
spring.datasource.data=import_entities.sql
|
|
||||||
|
|
||||||
spring.main.allow-bean-definition-overriding=true
|
spring.main.allow-bean-definition-overriding=true
|
||||||
|
|
||||||
spring.jpa.properties.hibernate.jdbc.batch_size=4
|
spring.jpa.properties.hibernate.jdbc.batch_size=4
|
||||||
|
|
|
@ -12,5 +12,3 @@ hibernate.cache.use_second_level_cache=true
|
||||||
hibernate.cache.use_query_cache=true
|
hibernate.cache.use_query_cache=true
|
||||||
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
|
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
|
||||||
|
|
||||||
# envers.X
|
|
||||||
envers.audit_table_suffix=_audit_log
|
|
|
@ -9,21 +9,17 @@ import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||||
|
|
||||||
import com.baeldung.batchinserts.CustomerController;
|
import com.baeldung.boot.Application;
|
||||||
import com.baeldung.batchinserts.repository.CustomerRepository;
|
import com.baeldung.boot.daos.CustomerRepository;
|
||||||
import com.baeldung.config.PersistenceConfiguration;
|
import com.baeldung.boot.web.controllers.CustomerController;
|
||||||
import com.baeldung.config.PersistenceProductConfiguration;
|
|
||||||
import com.baeldung.config.PersistenceUserConfiguration;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest
|
@SpringBootTest(classes=Application.class)
|
||||||
@AutoConfigureMockMvc
|
@AutoConfigureMockMvc
|
||||||
@ContextConfiguration(classes = { PersistenceConfiguration.class, PersistenceProductConfiguration.class, PersistenceUserConfiguration.class })
|
|
||||||
public class BatchInsertIntegrationTest {
|
public class BatchInsertIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
|
|
|
@ -1,13 +1,16 @@
|
||||||
package com.baeldung.dao.repositories;
|
package com.baeldung.boot.daos;
|
||||||
|
|
||||||
|
import com.baeldung.boot.Application;
|
||||||
|
import com.baeldung.boot.config.PersistenceConfiguration;
|
||||||
|
import com.baeldung.boot.daos.ArticleRepository;
|
||||||
|
import com.baeldung.boot.domain.Article;
|
||||||
|
|
||||||
import com.baeldung.config.PersistenceConfiguration;
|
|
||||||
import com.baeldung.config.PersistenceProductConfiguration;
|
|
||||||
import com.baeldung.config.PersistenceUserConfiguration;
|
|
||||||
import com.baeldung.domain.Article;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
@ -18,7 +21,7 @@ import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@DataJpaTest(excludeAutoConfiguration = {PersistenceConfiguration.class, PersistenceUserConfiguration.class, PersistenceProductConfiguration.class})
|
@DataJpaTest(properties="spring.datasource.data=classpath:import_entities.sql")
|
||||||
public class ArticleRepositoryIntegrationTest {
|
public class ArticleRepositoryIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
|
@ -1,7 +1,10 @@
|
||||||
package com.baeldung.dao.repositories;
|
package com.baeldung.boot.daos;
|
||||||
|
|
||||||
|
import com.baeldung.boot.Application;
|
||||||
|
import com.baeldung.boot.config.PersistenceConfiguration;
|
||||||
|
import com.baeldung.boot.daos.ExtendedStudentRepository;
|
||||||
|
import com.baeldung.boot.domain.Student;
|
||||||
|
|
||||||
import com.baeldung.config.PersistenceConfiguration;
|
|
||||||
import com.baeldung.domain.Student;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
@ -15,7 +18,7 @@ import java.util.List;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@ContextConfiguration(classes = {PersistenceConfiguration.class})
|
@ContextConfiguration(classes = {Application.class})
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
public class ExtendedStudentRepositoryIntegrationTest {
|
public class ExtendedStudentRepositoryIntegrationTest {
|
||||||
@Resource
|
@Resource
|
|
@ -1,11 +1,15 @@
|
||||||
package com.baeldung.dao.repositories;
|
package com.baeldung.boot.daos;
|
||||||
|
|
||||||
|
import com.baeldung.boot.Application;
|
||||||
|
import com.baeldung.boot.config.PersistenceConfiguration;
|
||||||
|
import com.baeldung.boot.daos.InventoryRepository;
|
||||||
|
import com.baeldung.boot.domain.MerchandiseEntity;
|
||||||
|
|
||||||
import com.baeldung.config.PersistenceConfiguration;
|
|
||||||
import com.baeldung.domain.MerchandiseEntity;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
@ -15,7 +19,7 @@ import static org.junit.Assert.assertNotEquals;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@DataJpaTest(excludeAutoConfiguration = {PersistenceConfiguration.class})
|
@SpringBootTest(classes=Application.class)
|
||||||
public class InventoryRepositoryIntegrationTest {
|
public class InventoryRepositoryIntegrationTest {
|
||||||
|
|
||||||
private static final String ORIGINAL_TITLE = "Pair of Pants";
|
private static final String ORIGINAL_TITLE = "Pair of Pants";
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.dao.repositories;
|
package com.baeldung.boot.daos;
|
||||||
|
|
||||||
import static junit.framework.TestCase.assertEquals;
|
import static junit.framework.TestCase.assertEquals;
|
||||||
import static junit.framework.TestCase.assertFalse;
|
import static junit.framework.TestCase.assertFalse;
|
||||||
|
@ -13,18 +13,24 @@ import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import com.baeldung.config.PersistenceConfiguration;
|
import com.baeldung.boot.Application;
|
||||||
import com.baeldung.config.PersistenceProductConfiguration;
|
import com.baeldung.boot.config.PersistenceConfiguration;
|
||||||
import com.baeldung.config.PersistenceUserConfiguration;
|
import com.baeldung.boot.daos.ItemTypeRepository;
|
||||||
import com.baeldung.domain.Item;
|
import com.baeldung.boot.daos.LocationRepository;
|
||||||
import com.baeldung.domain.ItemType;
|
import com.baeldung.boot.daos.ReadOnlyLocationRepository;
|
||||||
import com.baeldung.domain.Location;
|
import com.baeldung.boot.daos.StoreRepository;
|
||||||
import com.baeldung.domain.Store;
|
import com.baeldung.boot.domain.Item;
|
||||||
|
import com.baeldung.boot.domain.ItemType;
|
||||||
|
import com.baeldung.boot.domain.Location;
|
||||||
|
import com.baeldung.boot.domain.Store;
|
||||||
|
import com.baeldung.multipledb.PersistenceProductConfiguration;
|
||||||
|
import com.baeldung.multipledb.PersistenceUserConfiguration;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@DataJpaTest(excludeAutoConfiguration = { PersistenceConfiguration.class, PersistenceUserConfiguration.class, PersistenceProductConfiguration.class })
|
@DataJpaTest(properties="spring.datasource.data=classpath:import_entities.sql")
|
||||||
public class JpaRepositoriesIntegrationTest {
|
public class JpaRepositoriesIntegrationTest {
|
||||||
@Autowired
|
@Autowired
|
||||||
private LocationRepository locationRepository;
|
private LocationRepository locationRepository;
|
|
@ -1,7 +1,8 @@
|
||||||
package com.baeldung.dao.repositories;
|
package com.baeldung.boot.daos;
|
||||||
|
|
||||||
|
import com.baeldung.boot.daos.impl.PersonInsertRepository;
|
||||||
|
import com.baeldung.boot.domain.Person;
|
||||||
|
|
||||||
import com.baeldung.dao.repositories.impl.PersonInsertRepository;
|
|
||||||
import com.baeldung.domain.Person;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@ -1,7 +1,5 @@
|
||||||
package com.baeldung.dao.repositories;
|
package com.baeldung.boot.daos;
|
||||||
|
|
||||||
import com.baeldung.dao.repositories.user.UserRepository;
|
|
||||||
import com.baeldung.domain.user.User;
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -12,6 +10,9 @@ 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 com.baeldung.boot.daos.user.UserRepository;
|
||||||
|
import com.baeldung.boot.domain.User;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.Query;
|
import javax.persistence.Query;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
|
@ -1,7 +1,7 @@
|
||||||
package com.baeldung.dao.repositories;
|
package com.baeldung.boot.daos;
|
||||||
|
|
||||||
import com.baeldung.config.PersistenceConfiguration;
|
import com.baeldung.boot.Application;
|
||||||
import com.baeldung.domain.user.User;
|
import com.baeldung.boot.domain.User;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
@ -17,7 +17,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
* Created by adam.
|
* Created by adam.
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = PersistenceConfiguration.class)
|
@SpringBootTest(classes = Application.class)
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
public class UserRepositoryIntegrationTest extends UserRepositoryCommon {
|
public class UserRepositoryIntegrationTest extends UserRepositoryCommon {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package com.baeldung.dao.repositories;
|
package com.baeldung.boot.daos;
|
||||||
|
|
||||||
import com.baeldung.domain.user.User;
|
import com.baeldung.boot.Application;
|
||||||
|
import com.baeldung.boot.domain.User;
|
||||||
import com.baeldung.util.BaeldungPostgresqlContainer;
|
import com.baeldung.util.BaeldungPostgresqlContainer;
|
||||||
import org.junit.ClassRule;
|
import org.junit.ClassRule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -19,7 +20,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
* Created by adam.
|
* Created by adam.
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest
|
@SpringBootTest(classes = Application.class)
|
||||||
@ActiveProfiles({"tc", "tc-auto"})
|
@ActiveProfiles({"tc", "tc-auto"})
|
||||||
public class UserRepositoryTCAutoIntegrationTest extends UserRepositoryCommon {
|
public class UserRepositoryTCAutoIntegrationTest extends UserRepositoryCommon {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package com.baeldung.dao.repositories;
|
package com.baeldung.boot.daos;
|
||||||
|
|
||||||
import com.baeldung.domain.user.User;
|
import com.baeldung.boot.Application;
|
||||||
|
import com.baeldung.boot.domain.User;
|
||||||
import org.junit.ClassRule;
|
import org.junit.ClassRule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
@ -19,7 +20,7 @@ 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)
|
||||||
@SpringBootTest
|
@SpringBootTest(classes = Application.class)
|
||||||
@ActiveProfiles("tc")
|
@ActiveProfiles("tc")
|
||||||
@ContextConfiguration(initializers = {UserRepositoryTCIntegrationTest.Initializer.class})
|
@ContextConfiguration(initializers = {UserRepositoryTCIntegrationTest.Initializer.class})
|
||||||
public class UserRepositoryTCIntegrationTest extends UserRepositoryCommon {
|
public class UserRepositoryTCIntegrationTest extends UserRepositoryCommon {
|
|
@ -1,7 +1,7 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package com.baeldung.ddd.event;
|
package com.baeldung.boot.ddd.event;
|
||||||
|
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.Mockito.times;
|
import static org.mockito.Mockito.times;
|
||||||
|
@ -15,6 +15,10 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||||
|
|
||||||
|
import com.baeldung.boot.ddd.event.Aggregate2;
|
||||||
|
import com.baeldung.boot.ddd.event.Aggregate2Repository;
|
||||||
|
import com.baeldung.boot.ddd.event.DomainEvent;
|
||||||
|
|
||||||
@SpringJUnitConfig
|
@SpringJUnitConfig
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
class Aggregate2EventsIntegrationTest {
|
class Aggregate2EventsIntegrationTest {
|
|
@ -1,7 +1,7 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package com.baeldung.ddd.event;
|
package com.baeldung.boot.ddd.event;
|
||||||
|
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.Mockito.times;
|
import static org.mockito.Mockito.times;
|
||||||
|
@ -14,6 +14,10 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||||
|
|
||||||
|
import com.baeldung.boot.ddd.event.Aggregate3;
|
||||||
|
import com.baeldung.boot.ddd.event.Aggregate3Repository;
|
||||||
|
import com.baeldung.boot.ddd.event.DomainEvent;
|
||||||
|
|
||||||
@SpringJUnitConfig
|
@SpringJUnitConfig
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
class Aggregate3EventsIntegrationTest {
|
class Aggregate3EventsIntegrationTest {
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.ddd.event;
|
package com.baeldung.boot.ddd.event;
|
||||||
|
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.Mockito.times;
|
import static org.mockito.Mockito.times;
|
||||||
|
@ -16,6 +16,11 @@ import org.springframework.context.ApplicationEventPublisher;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||||
|
|
||||||
|
import com.baeldung.boot.ddd.event.Aggregate;
|
||||||
|
import com.baeldung.boot.ddd.event.AggregateRepository;
|
||||||
|
import com.baeldung.boot.ddd.event.DomainEvent;
|
||||||
|
import com.baeldung.boot.ddd.event.DomainService;
|
||||||
|
|
||||||
@SpringJUnitConfig
|
@SpringJUnitConfig
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
class AggregateEventsIntegrationTest {
|
class AggregateEventsIntegrationTest {
|
|
@ -1,10 +1,12 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package com.baeldung.ddd.event;
|
package com.baeldung.boot.ddd.event;
|
||||||
|
|
||||||
import org.springframework.transaction.event.TransactionalEventListener;
|
import org.springframework.transaction.event.TransactionalEventListener;
|
||||||
|
|
||||||
|
import com.baeldung.boot.ddd.event.DomainEvent;
|
||||||
|
|
||||||
interface TestEventHandler {
|
interface TestEventHandler {
|
||||||
@TransactionalEventListener
|
@TransactionalEventListener
|
||||||
void handleEvent(DomainEvent event);
|
void handleEvent(DomainEvent event);
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.passenger;
|
package com.baeldung.boot.passenger;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -12,6 +12,9 @@ import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import com.baeldung.boot.passenger.Passenger;
|
||||||
|
import com.baeldung.boot.passenger.PassengerRepository;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.PersistenceContext;
|
import javax.persistence.PersistenceContext;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -23,6 +26,7 @@ import static org.hamcrest.core.IsNot.not;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
|
||||||
@DataJpaTest
|
@DataJpaTest
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
public class PassengerRepositoryIntegrationTest {
|
public class PassengerRepositoryIntegrationTest {
|
|
@ -1,6 +1,7 @@
|
||||||
package com.baeldung.services;
|
package com.baeldung.boot.services;
|
||||||
|
|
||||||
import com.baeldung.domain.Foo;
|
import com.baeldung.boot.domain.Foo;
|
||||||
|
import com.baeldung.boot.services.IOperations;
|
||||||
import com.baeldung.util.IDUtil;
|
import com.baeldung.util.IDUtil;
|
||||||
import org.hamcrest.Matchers;
|
import org.hamcrest.Matchers;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
|
@ -1,11 +1,16 @@
|
||||||
package com.baeldung.services;
|
package com.baeldung.boot.services;
|
||||||
|
|
||||||
|
import com.baeldung.boot.Application;
|
||||||
|
import com.baeldung.boot.config.PersistenceConfiguration;
|
||||||
|
import com.baeldung.boot.domain.Foo;
|
||||||
|
import com.baeldung.boot.services.IFooService;
|
||||||
|
import com.baeldung.boot.services.IOperations;
|
||||||
|
|
||||||
import com.baeldung.config.PersistenceConfiguration;
|
|
||||||
import com.baeldung.domain.Foo;
|
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.dao.DataIntegrityViolationException;
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
@ -16,7 +21,7 @@ import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@ContextConfiguration(classes = {PersistenceConfiguration.class}, loader = AnnotationConfigContextLoader.class)
|
@SpringBootTest(classes=Application.class)
|
||||||
public class FooServicePersistenceIntegrationTest extends AbstractServicePersistenceIntegrationTest<Foo> {
|
public class FooServicePersistenceIntegrationTest extends AbstractServicePersistenceIntegrationTest<Foo> {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.services;
|
package com.baeldung.boot.services;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
@ -16,16 +16,19 @@ import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.security.test.context.support.WithMockUser;
|
import org.springframework.security.test.context.support.WithMockUser;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||||
|
|
||||||
import com.baeldung.config.PersistenceConfiguration;
|
import com.baeldung.boot.Application;
|
||||||
import com.baeldung.domain.Bar;
|
import com.baeldung.boot.config.PersistenceConfiguration;
|
||||||
|
import com.baeldung.boot.domain.Bar;
|
||||||
|
import com.baeldung.boot.services.IBarService;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@ContextConfiguration(classes = { PersistenceConfiguration.class }, loader = AnnotationConfigContextLoader.class)
|
@SpringBootTest(classes=Application.class)
|
||||||
public class SpringDataJPABarAuditIntegrationTest {
|
public class SpringDataJPABarAuditIntegrationTest {
|
||||||
|
|
||||||
private static Logger logger = LoggerFactory.getLogger(SpringDataJPABarAuditIntegrationTest.class);
|
private static Logger logger = LoggerFactory.getLogger(SpringDataJPABarAuditIntegrationTest.class);
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.services;
|
package com.baeldung.multipledb;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
@ -10,26 +10,23 @@ import java.util.Optional;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.dao.DataIntegrityViolationException;
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
import org.springframework.test.annotation.DirtiesContext;
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import com.baeldung.config.PersistenceProductConfiguration;
|
import com.baeldung.multipledb.dao.product.ProductRepository;
|
||||||
import com.baeldung.config.PersistenceUserConfiguration;
|
import com.baeldung.multipledb.dao.user.PossessionRepository;
|
||||||
import com.baeldung.dao.repositories.product.ProductRepository;
|
import com.baeldung.multipledb.dao.user.UserRepository;
|
||||||
import com.baeldung.dao.repositories.user.PossessionRepository;
|
import com.baeldung.multipledb.model.product.ProductMultipleDB;
|
||||||
import com.baeldung.dao.repositories.user.UserRepository;
|
import com.baeldung.multipledb.model.user.PossessionMultipleDB;
|
||||||
import com.baeldung.domain.product.Product;
|
import com.baeldung.multipledb.model.user.UserMultipleDB;
|
||||||
import com.baeldung.domain.user.Possession;
|
|
||||||
import com.baeldung.domain.user.User;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@ContextConfiguration(classes = { PersistenceUserConfiguration.class, PersistenceProductConfiguration.class })
|
@SpringBootTest(classes=MultipleDbApplication.class)
|
||||||
@EnableTransactionManagement
|
@EnableTransactionManagement
|
||||||
@DirtiesContext
|
|
||||||
public class JpaMultipleDBIntegrationTest {
|
public class JpaMultipleDBIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
|
@ -46,15 +43,15 @@ public class JpaMultipleDBIntegrationTest {
|
||||||
@Test
|
@Test
|
||||||
@Transactional("userTransactionManager")
|
@Transactional("userTransactionManager")
|
||||||
public void whenCreatingUser_thenCreated() {
|
public void whenCreatingUser_thenCreated() {
|
||||||
User user = new User();
|
UserMultipleDB user = new UserMultipleDB();
|
||||||
user.setName("John");
|
user.setName("John");
|
||||||
user.setEmail("john@test.com");
|
user.setEmail("john@test.com");
|
||||||
user.setAge(20);
|
user.setAge(20);
|
||||||
Possession p = new Possession("sample");
|
PossessionMultipleDB p = new PossessionMultipleDB("sample");
|
||||||
p = possessionRepository.save(p);
|
p = possessionRepository.save(p);
|
||||||
user.setPossessionList(Collections.singletonList(p));
|
user.setPossessionList(Collections.singletonList(p));
|
||||||
user = userRepository.save(user);
|
user = userRepository.save(user);
|
||||||
final Optional<User> result = userRepository.findById(user.getId());
|
final Optional<UserMultipleDB> result = userRepository.findById(user.getId());
|
||||||
assertTrue(result.isPresent());
|
assertTrue(result.isPresent());
|
||||||
System.out.println(result.get().getPossessionList());
|
System.out.println(result.get().getPossessionList());
|
||||||
assertEquals(1, result.get().getPossessionList().size());
|
assertEquals(1, result.get().getPossessionList().size());
|
||||||
|
@ -63,14 +60,14 @@ public class JpaMultipleDBIntegrationTest {
|
||||||
@Test
|
@Test
|
||||||
@Transactional("userTransactionManager")
|
@Transactional("userTransactionManager")
|
||||||
public void whenCreatingUsersWithSameEmail_thenRollback() {
|
public void whenCreatingUsersWithSameEmail_thenRollback() {
|
||||||
User user1 = new User();
|
UserMultipleDB user1 = new UserMultipleDB();
|
||||||
user1.setName("John");
|
user1.setName("John");
|
||||||
user1.setEmail("john@test.com");
|
user1.setEmail("john@test.com");
|
||||||
user1.setAge(20);
|
user1.setAge(20);
|
||||||
user1 = userRepository.save(user1);
|
user1 = userRepository.save(user1);
|
||||||
assertTrue(userRepository.findById(user1.getId()).isPresent());
|
assertTrue(userRepository.findById(user1.getId()).isPresent());
|
||||||
|
|
||||||
User user2 = new User();
|
UserMultipleDB user2 = new UserMultipleDB();
|
||||||
user2.setName("Tom");
|
user2.setName("Tom");
|
||||||
user2.setEmail("john@test.com");
|
user2.setEmail("john@test.com");
|
||||||
user2.setAge(10);
|
user2.setAge(10);
|
||||||
|
@ -88,7 +85,7 @@ public class JpaMultipleDBIntegrationTest {
|
||||||
@Test
|
@Test
|
||||||
@Transactional("productTransactionManager")
|
@Transactional("productTransactionManager")
|
||||||
public void whenCreatingProduct_thenCreated() {
|
public void whenCreatingProduct_thenCreated() {
|
||||||
Product product = new Product();
|
ProductMultipleDB product = new ProductMultipleDB();
|
||||||
product.setName("Book");
|
product.setName("Book");
|
||||||
product.setId(2);
|
product.setId(2);
|
||||||
product.setPrice(20);
|
product.setPrice(20);
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.dao.repositories.product;
|
package com.baeldung.multipledb;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.hasSize;
|
import static org.hamcrest.Matchers.hasSize;
|
||||||
|
@ -13,6 +13,7 @@ import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
|
@ -22,11 +23,12 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import com.baeldung.config.PersistenceProductConfiguration;
|
import com.baeldung.multipledb.PersistenceProductConfiguration;
|
||||||
import com.baeldung.domain.product.Product;
|
import com.baeldung.multipledb.dao.product.ProductRepository;
|
||||||
|
import com.baeldung.multipledb.model.product.ProductMultipleDB;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@ContextConfiguration(classes = { PersistenceProductConfiguration.class })
|
@SpringBootTest(classes=MultipleDbApplication.class)
|
||||||
@EnableTransactionManagement
|
@EnableTransactionManagement
|
||||||
public class ProductRepositoryIntegrationTest {
|
public class ProductRepositoryIntegrationTest {
|
||||||
|
|
||||||
|
@ -36,22 +38,22 @@ public class ProductRepositoryIntegrationTest {
|
||||||
@Before
|
@Before
|
||||||
@Transactional("productTransactionManager")
|
@Transactional("productTransactionManager")
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
productRepository.save(Product.from(1001, "Book", 21));
|
productRepository.save(ProductMultipleDB.from(1001, "Book", 21));
|
||||||
productRepository.save(Product.from(1002, "Coffee", 10));
|
productRepository.save(ProductMultipleDB.from(1002, "Coffee", 10));
|
||||||
productRepository.save(Product.from(1003, "Jeans", 30));
|
productRepository.save(ProductMultipleDB.from(1003, "Jeans", 30));
|
||||||
productRepository.save(Product.from(1004, "Shirt", 32));
|
productRepository.save(ProductMultipleDB.from(1004, "Shirt", 32));
|
||||||
productRepository.save(Product.from(1005, "Bacon", 10));
|
productRepository.save(ProductMultipleDB.from(1005, "Bacon", 10));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenRequestingFirstPageOfSizeTwo_ThenReturnFirstPage() {
|
public void whenRequestingFirstPageOfSizeTwo_ThenReturnFirstPage() {
|
||||||
Pageable pageRequest = PageRequest.of(0, 2);
|
Pageable pageRequest = PageRequest.of(0, 2);
|
||||||
|
|
||||||
Page<Product> result = productRepository.findAll(pageRequest);
|
Page<ProductMultipleDB> result = productRepository.findAll(pageRequest);
|
||||||
|
|
||||||
assertThat(result.getContent(), hasSize(2));
|
assertThat(result.getContent(), hasSize(2));
|
||||||
assertTrue(result.stream()
|
assertTrue(result.stream()
|
||||||
.map(Product::getId)
|
.map(ProductMultipleDB::getId)
|
||||||
.allMatch(id -> Arrays.asList(1001, 1002)
|
.allMatch(id -> Arrays.asList(1001, 1002)
|
||||||
.contains(id)));
|
.contains(id)));
|
||||||
}
|
}
|
||||||
|
@ -60,11 +62,11 @@ public class ProductRepositoryIntegrationTest {
|
||||||
public void whenRequestingSecondPageOfSizeTwo_ThenReturnSecondPage() {
|
public void whenRequestingSecondPageOfSizeTwo_ThenReturnSecondPage() {
|
||||||
Pageable pageRequest = PageRequest.of(1, 2);
|
Pageable pageRequest = PageRequest.of(1, 2);
|
||||||
|
|
||||||
Page<Product> result = productRepository.findAll(pageRequest);
|
Page<ProductMultipleDB> result = productRepository.findAll(pageRequest);
|
||||||
|
|
||||||
assertThat(result.getContent(), hasSize(2));
|
assertThat(result.getContent(), hasSize(2));
|
||||||
assertTrue(result.stream()
|
assertTrue(result.stream()
|
||||||
.map(Product::getId)
|
.map(ProductMultipleDB::getId)
|
||||||
.allMatch(id -> Arrays.asList(1003, 1004)
|
.allMatch(id -> Arrays.asList(1003, 1004)
|
||||||
.contains(id)));
|
.contains(id)));
|
||||||
}
|
}
|
||||||
|
@ -73,11 +75,11 @@ public class ProductRepositoryIntegrationTest {
|
||||||
public void whenRequestingLastPage_ThenReturnLastPageWithRemData() {
|
public void whenRequestingLastPage_ThenReturnLastPageWithRemData() {
|
||||||
Pageable pageRequest = PageRequest.of(2, 2);
|
Pageable pageRequest = PageRequest.of(2, 2);
|
||||||
|
|
||||||
Page<Product> result = productRepository.findAll(pageRequest);
|
Page<ProductMultipleDB> result = productRepository.findAll(pageRequest);
|
||||||
|
|
||||||
assertThat(result.getContent(), hasSize(1));
|
assertThat(result.getContent(), hasSize(1));
|
||||||
assertTrue(result.stream()
|
assertTrue(result.stream()
|
||||||
.map(Product::getId)
|
.map(ProductMultipleDB::getId)
|
||||||
.allMatch(id -> Arrays.asList(1005)
|
.allMatch(id -> Arrays.asList(1005)
|
||||||
.contains(id)));
|
.contains(id)));
|
||||||
}
|
}
|
||||||
|
@ -86,12 +88,12 @@ public class ProductRepositoryIntegrationTest {
|
||||||
public void whenSortingByNameAscAndPaging_ThenReturnSortedPagedResult() {
|
public void whenSortingByNameAscAndPaging_ThenReturnSortedPagedResult() {
|
||||||
Pageable pageRequest = PageRequest.of(0, 3, Sort.by("name"));
|
Pageable pageRequest = PageRequest.of(0, 3, Sort.by("name"));
|
||||||
|
|
||||||
Page<Product> result = productRepository.findAll(pageRequest);
|
Page<ProductMultipleDB> result = productRepository.findAll(pageRequest);
|
||||||
|
|
||||||
assertThat(result.getContent(), hasSize(3));
|
assertThat(result.getContent(), hasSize(3));
|
||||||
assertThat(result.getContent()
|
assertThat(result.getContent()
|
||||||
.stream()
|
.stream()
|
||||||
.map(Product::getId)
|
.map(ProductMultipleDB::getId)
|
||||||
.collect(Collectors.toList()), equalTo(Arrays.asList(1005, 1001, 1002)));
|
.collect(Collectors.toList()), equalTo(Arrays.asList(1005, 1001, 1002)));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -101,12 +103,12 @@ public class ProductRepositoryIntegrationTest {
|
||||||
Pageable pageRequest = PageRequest.of(0, 3, Sort.by("price")
|
Pageable pageRequest = PageRequest.of(0, 3, Sort.by("price")
|
||||||
.descending());
|
.descending());
|
||||||
|
|
||||||
Page<Product> result = productRepository.findAll(pageRequest);
|
Page<ProductMultipleDB> result = productRepository.findAll(pageRequest);
|
||||||
|
|
||||||
assertThat(result.getContent(), hasSize(3));
|
assertThat(result.getContent(), hasSize(3));
|
||||||
assertThat(result.getContent()
|
assertThat(result.getContent()
|
||||||
.stream()
|
.stream()
|
||||||
.map(Product::getId)
|
.map(ProductMultipleDB::getId)
|
||||||
.collect(Collectors.toList()), equalTo(Arrays.asList(1004, 1003, 1001)));
|
.collect(Collectors.toList()), equalTo(Arrays.asList(1004, 1003, 1001)));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -117,12 +119,12 @@ public class ProductRepositoryIntegrationTest {
|
||||||
.descending()
|
.descending()
|
||||||
.and(Sort.by("name")));
|
.and(Sort.by("name")));
|
||||||
|
|
||||||
Page<Product> result = productRepository.findAll(pageRequest);
|
Page<ProductMultipleDB> result = productRepository.findAll(pageRequest);
|
||||||
|
|
||||||
assertThat(result.getContent(), hasSize(5));
|
assertThat(result.getContent(), hasSize(5));
|
||||||
assertThat(result.getContent()
|
assertThat(result.getContent()
|
||||||
.stream()
|
.stream()
|
||||||
.map(Product::getId)
|
.map(ProductMultipleDB::getId)
|
||||||
.collect(Collectors.toList()), equalTo(Arrays.asList(1004, 1003, 1001, 1005, 1002)));
|
.collect(Collectors.toList()), equalTo(Arrays.asList(1004, 1003, 1001, 1005, 1002)));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -131,11 +133,11 @@ public class ProductRepositoryIntegrationTest {
|
||||||
public void whenRequestingFirstPageOfSizeTwoUsingCustomMethod_ThenReturnFirstPage() {
|
public void whenRequestingFirstPageOfSizeTwoUsingCustomMethod_ThenReturnFirstPage() {
|
||||||
Pageable pageRequest = PageRequest.of(0, 2);
|
Pageable pageRequest = PageRequest.of(0, 2);
|
||||||
|
|
||||||
List<Product> result = productRepository.findAllByPrice(10, pageRequest);
|
List<ProductMultipleDB> result = productRepository.findAllByPrice(10, pageRequest);
|
||||||
|
|
||||||
assertThat(result, hasSize(2));
|
assertThat(result, hasSize(2));
|
||||||
assertTrue(result.stream()
|
assertTrue(result.stream()
|
||||||
.map(Product::getId)
|
.map(ProductMultipleDB::getId)
|
||||||
.allMatch(id -> Arrays.asList(1002, 1005)
|
.allMatch(id -> Arrays.asList(1002, 1005)
|
||||||
.contains(id)));
|
.contains(id)));
|
||||||
}
|
}
|
|
@ -5,7 +5,7 @@ import org.junit.runner.RunWith;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import com.baeldung.Application;
|
import com.baeldung.boot.Application;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = Application.class)
|
@SpringBootTest(classes = Application.class)
|
||||||
|
|
|
@ -6,10 +6,10 @@ import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import com.baeldung.Application;
|
import com.baeldung.boot.Application;
|
||||||
import com.baeldung.config.PersistenceConfiguration;
|
import com.baeldung.boot.config.PersistenceConfiguration;
|
||||||
import com.baeldung.config.PersistenceProductConfiguration;
|
import com.baeldung.multipledb.PersistenceProductConfiguration;
|
||||||
import com.baeldung.config.PersistenceUserConfiguration;
|
import com.baeldung.multipledb.PersistenceUserConfiguration;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@DataJpaTest(excludeAutoConfiguration = {
|
@DataJpaTest(excludeAutoConfiguration = {
|
||||||
|
|
Loading…
Reference in New Issue