fix spring-data-jpa module
This commit is contained in:
parent
6bae0fc28a
commit
70016f1d76
|
@ -80,5 +80,9 @@
|
|||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<start-class>com.baeldung.boot.Application</start-class>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1,10 +1,11 @@
|
|||
package com.baeldung;
|
||||
package com.baeldung.boot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
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
|
||||
@EnableJpaRepositories(repositoryBaseClass = ExtendedRepositoryImpl.class)
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.boot.config;
|
||||
|
||||
import com.baeldung.boot.daos.impl.ExtendedRepositoryImpl;
|
||||
import com.baeldung.boot.services.IBarService;
|
||||
import com.baeldung.boot.services.impl.BarSpringDataJpaService;
|
||||
import com.google.common.base.Preconditions;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.*;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.JpaVendorAdapter;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.Properties;
|
||||
|
||||
@Configuration
|
||||
@Profile("!tc")
|
||||
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.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import com.baeldung.boot.domain.Article;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
package com.baeldung.dao.repositories;
|
||||
package com.baeldung.boot.daos;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.domain.Item;
|
||||
import com.baeldung.boot.domain.Item;
|
||||
|
||||
@Repository
|
||||
public interface CustomItemRepository {
|
|
@ -1,8 +1,8 @@
|
|||
package com.baeldung.dao.repositories;
|
||||
package com.baeldung.boot.daos;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.domain.ItemType;
|
||||
import com.baeldung.boot.domain.ItemType;
|
||||
|
||||
@Repository
|
||||
public interface CustomItemTypeRepository {
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.dao.repositories;
|
||||
package com.baeldung.boot.daos;
|
||||
|
||||
import java.io.Serializable;
|
||||
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> {
|
||||
}
|
|
@ -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 com.baeldung.boot.domain.Bar;
|
||||
|
||||
import java.io.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.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import com.baeldung.boot.domain.Foo;
|
||||
|
||||
public interface IFooDao extends JpaRepository<Foo, Long> {
|
||||
|
||||
@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 com.baeldung.boot.domain.MerchandiseEntity;
|
||||
|
||||
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.stereotype.Repository;
|
||||
|
||||
import com.baeldung.domain.ItemType;
|
||||
import com.baeldung.boot.domain.ItemType;
|
||||
|
||||
@Repository
|
||||
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.stereotype.Repository;
|
||||
|
||||
import com.baeldung.domain.Location;
|
||||
import com.baeldung.boot.domain.Location;
|
||||
|
||||
@Repository
|
||||
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 org.springframework.data.repository.Repository;
|
||||
|
||||
import com.baeldung.domain.Location;
|
||||
import com.baeldung.boot.domain.Location;
|
||||
|
||||
@org.springframework.stereotype.Repository
|
||||
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 org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.domain.Store;
|
||||
import com.baeldung.boot.domain.Store;
|
||||
|
||||
@Repository
|
||||
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 org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.domain.Item;
|
||||
import com.baeldung.dao.repositories.CustomItemRepository;
|
||||
import com.baeldung.boot.daos.CustomItemRepository;
|
||||
import com.baeldung.boot.domain.Item;
|
||||
|
||||
@Repository
|
||||
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;
|
||||
|
||||
|
@ -7,8 +7,8 @@ import org.slf4j.LoggerFactory;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.domain.ItemType;
|
||||
import com.baeldung.dao.repositories.CustomItemTypeRepository;
|
||||
import com.baeldung.boot.daos.CustomItemTypeRepository;
|
||||
import com.baeldung.boot.domain.ItemType;
|
||||
|
||||
@Repository
|
||||
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.util.List;
|
||||
|
@ -10,10 +10,11 @@ import javax.persistence.criteria.CriteriaQuery;
|
|||
import javax.persistence.criteria.Root;
|
||||
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.SimpleJpaRepository;
|
||||
|
||||
import com.baeldung.boot.daos.ExtendedRepository;
|
||||
|
||||
public class ExtendedRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements ExtendedRepository<T, ID> {
|
||||
|
||||
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 com.baeldung.boot.domain.Person;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.transaction.Transactional;
|
|
@ -1,7 +1,7 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.ddd.event;
|
||||
package com.baeldung.boot.ddd.event;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
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.Collection;
|
|
@ -1,7 +1,7 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.ddd.event;
|
||||
package com.baeldung.boot.ddd.event;
|
||||
|
||||
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.GeneratedValue;
|
|
@ -1,7 +1,7 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.ddd.event;
|
||||
package com.baeldung.boot.ddd.event;
|
||||
|
||||
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;
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.ddd.event;
|
||||
package com.baeldung.boot.ddd.event;
|
||||
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
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;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.domain;
|
||||
package com.baeldung.boot.domain;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Date;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.domain;
|
||||
package com.baeldung.boot.domain;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import org.hibernate.annotations.OrderBy;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.domain;
|
||||
package com.baeldung.boot.domain;
|
||||
|
||||
import org.hibernate.envers.Audited;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.domain;
|
||||
package com.baeldung.boot.domain;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.domain;
|
||||
package com.baeldung.boot.domain;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.domain;
|
||||
package com.baeldung.boot.domain;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.domain;
|
||||
package com.baeldung.boot.domain;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.domain;
|
||||
package com.baeldung.boot.domain;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.domain;
|
||||
package com.baeldung.boot.domain;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.domain.user;
|
||||
package com.baeldung.boot.domain;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.domain;
|
||||
package com.baeldung.boot.domain;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.domain;
|
||||
package com.baeldung.boot.domain;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.domain;
|
||||
package com.baeldung.boot.domain;
|
||||
|
||||
import javax.persistence.ElementCollection;
|
||||
import javax.persistence.Entity;
|
|
@ -0,0 +1,86 @@
|
|||
package com.baeldung.boot.domain;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
|
||||
@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<Possession> possessionList;
|
||||
|
||||
public User() {
|
||||
super();
|
||||
}
|
||||
|
||||
public User(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<Possession> getPossessionList() {
|
||||
return possessionList;
|
||||
}
|
||||
|
||||
public void setPossessionList(List<Possession> possessionList) {
|
||||
this.possessionList = possessionList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append("User [name=").append(name).append(", id=").append(id).append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.passenger;
|
||||
package com.baeldung.boot.passenger;
|
||||
|
||||
import java.util.List;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.passenger;
|
||||
package com.baeldung.boot.passenger;
|
||||
|
||||
import javax.persistence.Basic;
|
||||
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.jpa.repository.JpaRepository;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.passenger;
|
||||
package com.baeldung.boot.passenger;
|
||||
|
||||
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.Pageable;
|
||||
|
||||
import com.baeldung.boot.domain.Foo;
|
||||
|
||||
public interface IFooService extends IOperations<Foo> {
|
||||
|
||||
Foo retrieveByName(String name);
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.services;
|
||||
package com.baeldung.boot.services;
|
||||
|
||||
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 org.springframework.data.domain.Page;
|
||||
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 org.springframework.data.repository.CrudRepository;
|
||||
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.data.domain.Page;
|
||||
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.baeldung.dao.IFooDao;
|
||||
import com.baeldung.domain.Foo;
|
||||
import com.baeldung.services.IFooService;
|
||||
import com.baeldung.boot.daos.IFooDao;
|
||||
import com.baeldung.boot.domain.Foo;
|
||||
import com.baeldung.boot.services.IFooService;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
|
@ -1,8 +1,9 @@
|
|||
package com.baeldung.dao.repositories.user;
|
||||
package com.baeldung.boot.user;
|
||||
|
||||
import com.baeldung.domain.user.Possession;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import com.baeldung.boot.domain.Possession;
|
||||
|
||||
public interface PossessionRepository extends JpaRepository<Possession, Long> {
|
||||
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
package com.baeldung.boot.user;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import com.baeldung.boot.domain.User;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public interface UserRepository extends JpaRepository<User, Integer> , UserRepositoryCustom{
|
||||
|
||||
Stream<User> findAllByName(String name);
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.status = 1")
|
||||
Collection<User> findAllActiveUsers();
|
||||
|
||||
@Query(value = "SELECT * FROM Users u WHERE u.status = 1", nativeQuery = true)
|
||||
Collection<User> findAllActiveUsersNative();
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.status = ?1")
|
||||
User findUserByStatus(Integer status);
|
||||
|
||||
@Query(value = "SELECT * FROM Users u WHERE u.status = ?1", nativeQuery = true)
|
||||
User findUserByStatusNative(Integer status);
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.status = ?1 and u.name = ?2")
|
||||
User findUserByStatusAndName(Integer status, String name);
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.status = :status and u.name = :name")
|
||||
User findUserByStatusAndNameNamedParams(@Param("status") Integer status, @Param("name") String name);
|
||||
|
||||
@Query(value = "SELECT * FROM Users u WHERE u.status = :status AND u.name = :name", nativeQuery = true)
|
||||
User findUserByStatusAndNameNamedParamsNative(@Param("status") Integer status, @Param("name") String name);
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.status = :status and u.name = :name")
|
||||
User findUserByUserStatusAndUserName(@Param("status") Integer userStatus, @Param("name") String userName);
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.name like ?1%")
|
||||
User findUserByNameLike(String name);
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.name like :name%")
|
||||
User findUserByNameLikeNamedParam(@Param("name") String name);
|
||||
|
||||
@Query(value = "SELECT * FROM users u WHERE u.name LIKE ?1%", nativeQuery = true)
|
||||
User findUserByNameLikeNative(String name);
|
||||
|
||||
@Query(value = "SELECT u FROM User u")
|
||||
List<User> findAllUsers(Sort sort);
|
||||
|
||||
@Query(value = "SELECT u FROM User u ORDER BY id")
|
||||
Page<User> findAllUsersWithPagination(Pageable pageable);
|
||||
|
||||
@Query(value = "SELECT * FROM Users ORDER BY id", countQuery = "SELECT count(*) FROM Users", nativeQuery = true)
|
||||
Page<User> findAllUsersWithPaginationNative(Pageable pageable);
|
||||
|
||||
@Modifying
|
||||
@Query("update User u set u.status = :status where u.name = :name")
|
||||
int updateUserSetStatusForName(@Param("status") Integer status, @Param("name") String name);
|
||||
|
||||
@Modifying
|
||||
@Query(value = "UPDATE Users u SET u.status = ? WHERE u.name = ?", nativeQuery = true)
|
||||
int updateUserSetStatusForNameNative(Integer status, String name);
|
||||
|
||||
@Query(value = "INSERT INTO Users (name, age, email, status) VALUES (:name, :age, :email, :status)", nativeQuery = true)
|
||||
@Modifying
|
||||
void insertUser(@Param("name") String name, @Param("age") Integer age, @Param("status") Integer status, @Param("email") String email);
|
||||
|
||||
@Modifying
|
||||
@Query(value = "UPDATE Users u SET status = ? WHERE u.name = ?", nativeQuery = true)
|
||||
int updateUserSetStatusForNameNativePostgres(Integer status, String name);
|
||||
|
||||
@Query(value = "SELECT u FROM User u WHERE u.name IN :names")
|
||||
List<User> findUserByNameList(@Param("names") Collection<String> names);
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.boot.user;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import com.baeldung.boot.domain.User;
|
||||
|
||||
public interface UserRepositoryCustom {
|
||||
List<User> findUserByEmails(Set<String> emails);
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package com.baeldung.boot.user;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Path;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import com.baeldung.boot.domain.User;
|
||||
|
||||
public class UserRepositoryCustomImpl implements UserRepositoryCustom {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Override
|
||||
public List<User> findUserByEmails(Set<String> emails) {
|
||||
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<User> query = cb.createQuery(User.class);
|
||||
Root<User> user = query.from(User.class);
|
||||
|
||||
Path<String> emailPath = user.get("email");
|
||||
|
||||
List<Predicate> predicates = new ArrayList<>();
|
||||
for (String email : emails) {
|
||||
|
||||
predicates.add(cb.like(emailPath, email));
|
||||
|
||||
}
|
||||
query.select(user)
|
||||
.where(cb.or(predicates.toArray(new Predicate[predicates.size()])));
|
||||
|
||||
return entityManager.createQuery(query)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
}
|
|
@ -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,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 org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -17,9 +17,9 @@ import org.springframework.transaction.PlatformTransactionManager;
|
|||
import javax.sql.DataSource;
|
||||
import java.util.HashMap;
|
||||
|
||||
@Configuration
|
||||
//@Configuration
|
||||
@PropertySource({"classpath:persistence-multiple-db.properties"})
|
||||
@EnableJpaRepositories(basePackages = "com.baeldung.dao.repositories.product", entityManagerFactoryRef = "productEntityManager", transactionManagerRef = "productTransactionManager")
|
||||
@EnableJpaRepositories(basePackages = "com.baeldung.multipledb", entityManagerFactoryRef = "productEntityManager", transactionManagerRef = "productTransactionManager")
|
||||
@Profile("!tc")
|
||||
public class PersistenceProductConfiguration {
|
||||
@Autowired
|
||||
|
@ -35,7 +35,7 @@ public class PersistenceProductConfiguration {
|
|||
public LocalContainerEntityManagerFactoryBean productEntityManager() {
|
||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
em.setDataSource(productDataSource());
|
||||
em.setPackagesToScan("com.baeldung.domain.product");
|
||||
em.setPackagesToScan("com.baeldung.multipledb");
|
||||
|
||||
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||
em.setJpaVendorAdapter(vendorAdapter);
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.config;
|
||||
package com.baeldung.multipledb;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -14,9 +14,9 @@ import org.springframework.transaction.PlatformTransactionManager;
|
|||
import javax.sql.DataSource;
|
||||
import java.util.HashMap;
|
||||
|
||||
@Configuration
|
||||
//@Configuration
|
||||
@PropertySource({"classpath:persistence-multiple-db.properties"})
|
||||
@EnableJpaRepositories(basePackages = "com.baeldung.dao.repositories.user", entityManagerFactoryRef = "userEntityManager", transactionManagerRef = "userTransactionManager")
|
||||
@EnableJpaRepositories(basePackages = "com.baeldung.multipledb", entityManagerFactoryRef = "userEntityManager", transactionManagerRef = "userTransactionManager")
|
||||
@Profile("!tc")
|
||||
public class PersistenceUserConfiguration {
|
||||
@Autowired
|
||||
|
@ -33,7 +33,7 @@ public class PersistenceUserConfiguration {
|
|||
public LocalContainerEntityManagerFactoryBean userEntityManager() {
|
||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
em.setDataSource(userDataSource());
|
||||
em.setPackagesToScan("com.baeldung.domain.user");
|
||||
em.setPackagesToScan("com.baeldung.multipledb");
|
||||
|
||||
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||
em.setJpaVendorAdapter(vendorAdapter);
|
|
@ -0,0 +1,82 @@
|
|||
package com.baeldung.multipledb;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table
|
||||
public class Possession {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
public Possession() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Possession(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 Possession other = (Possession) 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,9 @@
|
|||
package com.baeldung.multipledb;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import com.baeldung.boot.domain.Possession;
|
||||
|
||||
public interface PossessionRepository extends JpaRepository<Possession, Long> {
|
||||
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.domain.product;
|
||||
package com.baeldung.multipledb;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
|
@ -1,6 +1,4 @@
|
|||
package com.baeldung.dao.repositories.product;
|
||||
|
||||
import com.baeldung.domain.product.Product;
|
||||
package com.baeldung.multipledb;
|
||||
|
||||
import java.util.List;
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
package com.baeldung.multipledb;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
|
||||
@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;
|
||||
|
||||
public User() {
|
||||
super();
|
||||
}
|
||||
|
||||
public User(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;
|
||||
}
|
||||
|
||||
@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,17 +1,9 @@
|
|||
# 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
|
||||
#spring.datasource.data=import_entities.sql
|
||||
#spring.jpa.properties.hibernate.hbm2ddl.import_files=import_entities.sql
|
||||
|
||||
# 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.main.allow-bean-definition-overriding=true
|
||||
|
||||
spring.datasource.data=import_entities.sql
|
||||
|
||||
spring.main.allow-bean-definition-overriding=true
|
||||
# envers.X
|
||||
#spring.jpa.properties.hibernate.hbm2ddl.import_files=import_entities.sql
|
||||
spring.datasource.data=classpath:import_entities.sql
|
|
@ -12,5 +12,3 @@ hibernate.cache.use_second_level_cache=true
|
|||
hibernate.cache.use_query_cache=true
|
||||
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
|
||||
|
||||
# envers.X
|
||||
envers.audit_table_suffix=_audit_log
|
|
@ -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.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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 java.text.SimpleDateFormat;
|
||||
|
@ -18,7 +21,7 @@ import static org.junit.Assert.assertEquals;
|
|||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@DataJpaTest(excludeAutoConfiguration = {PersistenceConfiguration.class, PersistenceUserConfiguration.class, PersistenceProductConfiguration.class})
|
||||
@DataJpaTest(properties="spring.datasource.data=classpath:import_entities.sql")
|
||||
public class ArticleRepositoryIntegrationTest {
|
||||
|
||||
@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.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
@ -15,7 +18,7 @@ import java.util.List;
|
|||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = {PersistenceConfiguration.class})
|
||||
@ContextConfiguration(classes = {Application.class})
|
||||
@DirtiesContext
|
||||
public class ExtendedStudentRepositoryIntegrationTest {
|
||||
@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.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
@ -15,7 +19,7 @@ import static org.junit.Assert.assertNotEquals;
|
|||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@DataJpaTest(excludeAutoConfiguration = {PersistenceConfiguration.class})
|
||||
@SpringBootTest(classes=Application.class)
|
||||
public class InventoryRepositoryIntegrationTest {
|
||||
|
||||
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.assertFalse;
|
||||
|
@ -13,18 +13,24 @@ import org.junit.Test;
|
|||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.config.PersistenceConfiguration;
|
||||
import com.baeldung.config.PersistenceProductConfiguration;
|
||||
import com.baeldung.config.PersistenceUserConfiguration;
|
||||
import com.baeldung.domain.Item;
|
||||
import com.baeldung.domain.ItemType;
|
||||
import com.baeldung.domain.Location;
|
||||
import com.baeldung.domain.Store;
|
||||
import com.baeldung.boot.Application;
|
||||
import com.baeldung.boot.config.PersistenceConfiguration;
|
||||
import com.baeldung.boot.daos.ItemTypeRepository;
|
||||
import com.baeldung.boot.daos.LocationRepository;
|
||||
import com.baeldung.boot.daos.ReadOnlyLocationRepository;
|
||||
import com.baeldung.boot.daos.StoreRepository;
|
||||
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)
|
||||
@DataJpaTest(excludeAutoConfiguration = { PersistenceConfiguration.class, PersistenceUserConfiguration.class, PersistenceProductConfiguration.class })
|
||||
@DataJpaTest(properties="spring.datasource.data=classpath:import_entities.sql")
|
||||
public class JpaRepositoriesIntegrationTest {
|
||||
@Autowired
|
||||
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.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
@ -0,0 +1,405 @@
|
|||
package com.baeldung.boot.daos;
|
||||
|
||||
import com.baeldung.boot.domain.User;
|
||||
import com.baeldung.boot.user.UserRepository;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.domain.JpaSort;
|
||||
import org.springframework.data.mapping.PropertyReferenceException;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class UserRepositoryCommon {
|
||||
|
||||
final String USER_EMAIL = "email@example.com";
|
||||
final String USER_EMAIL2 = "email2@example.com";
|
||||
final String USER_EMAIL3 = "email3@example.com";
|
||||
final String USER_EMAIL4 = "email4@example.com";
|
||||
final Integer INACTIVE_STATUS = 0;
|
||||
final Integer ACTIVE_STATUS = 1;
|
||||
final String USER_EMAIL5 = "email5@example.com";
|
||||
final String USER_EMAIL6 = "email6@example.com";
|
||||
final String USER_NAME_ADAM = "Adam";
|
||||
final String USER_NAME_PETER = "Peter";
|
||||
|
||||
@Autowired
|
||||
protected UserRepository userRepository;
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenUsersWithSameNameInDB_WhenFindAllByName_ThenReturnStreamOfUsers() {
|
||||
User user1 = new User();
|
||||
user1.setName(USER_NAME_ADAM);
|
||||
user1.setEmail(USER_EMAIL);
|
||||
userRepository.save(user1);
|
||||
|
||||
User user2 = new User();
|
||||
user2.setName(USER_NAME_ADAM);
|
||||
user2.setEmail(USER_EMAIL2);
|
||||
userRepository.save(user2);
|
||||
|
||||
User user3 = new User();
|
||||
user3.setName(USER_NAME_ADAM);
|
||||
user3.setEmail(USER_EMAIL3);
|
||||
userRepository.save(user3);
|
||||
|
||||
User user4 = new User();
|
||||
user4.setName("SAMPLE");
|
||||
user4.setEmail(USER_EMAIL4);
|
||||
userRepository.save(user4);
|
||||
|
||||
try (Stream<User> foundUsersStream = userRepository.findAllByName(USER_NAME_ADAM)) {
|
||||
assertThat(foundUsersStream.count()).isEqualTo(3l);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsersInDB_WhenFindAllWithQueryAnnotation_ThenReturnCollectionWithActiveUsers() {
|
||||
User user1 = new User();
|
||||
user1.setName(USER_NAME_ADAM);
|
||||
user1.setEmail(USER_EMAIL);
|
||||
user1.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user1);
|
||||
|
||||
User user2 = new User();
|
||||
user2.setName(USER_NAME_ADAM);
|
||||
user2.setEmail(USER_EMAIL2);
|
||||
user2.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user2);
|
||||
|
||||
User user3 = new User();
|
||||
user3.setName(USER_NAME_ADAM);
|
||||
user3.setEmail(USER_EMAIL3);
|
||||
user3.setStatus(INACTIVE_STATUS);
|
||||
userRepository.save(user3);
|
||||
|
||||
Collection<User> allActiveUsers = userRepository.findAllActiveUsers();
|
||||
|
||||
assertThat(allActiveUsers.size()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsersInDB_WhenFindAllWithQueryAnnotationNative_ThenReturnCollectionWithActiveUsers() {
|
||||
User user1 = new User();
|
||||
user1.setName(USER_NAME_ADAM);
|
||||
user1.setEmail(USER_EMAIL);
|
||||
user1.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user1);
|
||||
|
||||
User user2 = new User();
|
||||
user2.setName(USER_NAME_ADAM);
|
||||
user2.setEmail(USER_EMAIL2);
|
||||
user2.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user2);
|
||||
|
||||
User user3 = new User();
|
||||
user3.setName(USER_NAME_ADAM);
|
||||
user3.setEmail(USER_EMAIL3);
|
||||
user3.setStatus(INACTIVE_STATUS);
|
||||
userRepository.save(user3);
|
||||
|
||||
Collection<User> allActiveUsers = userRepository.findAllActiveUsersNative();
|
||||
|
||||
assertThat(allActiveUsers.size()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserInDB_WhenFindUserByStatusWithQueryAnnotation_ThenReturnActiveUser() {
|
||||
User user = new User();
|
||||
user.setName(USER_NAME_ADAM);
|
||||
user.setEmail(USER_EMAIL);
|
||||
user.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user);
|
||||
|
||||
User userByStatus = userRepository.findUserByStatus(ACTIVE_STATUS);
|
||||
|
||||
assertThat(userByStatus.getName()).isEqualTo(USER_NAME_ADAM);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserInDB_WhenFindUserByStatusWithQueryAnnotationNative_ThenReturnActiveUser() {
|
||||
User user = new User();
|
||||
user.setName(USER_NAME_ADAM);
|
||||
user.setEmail(USER_EMAIL);
|
||||
user.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user);
|
||||
|
||||
User userByStatus = userRepository.findUserByStatusNative(ACTIVE_STATUS);
|
||||
|
||||
assertThat(userByStatus.getName()).isEqualTo(USER_NAME_ADAM);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsersInDB_WhenFindUserByStatusAndNameWithQueryAnnotationIndexedParams_ThenReturnOneUser() {
|
||||
User user = new User();
|
||||
user.setName(USER_NAME_ADAM);
|
||||
user.setEmail(USER_EMAIL);
|
||||
user.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user);
|
||||
|
||||
User user2 = new User();
|
||||
user2.setName(USER_NAME_PETER);
|
||||
user2.setEmail(USER_EMAIL2);
|
||||
user2.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user2);
|
||||
|
||||
User userByStatus = userRepository.findUserByStatusAndName(ACTIVE_STATUS, USER_NAME_ADAM);
|
||||
|
||||
assertThat(userByStatus.getName()).isEqualTo(USER_NAME_ADAM);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsersInDB_WhenFindUserByStatusAndNameWithQueryAnnotationNamedParams_ThenReturnOneUser() {
|
||||
User user = new User();
|
||||
user.setName(USER_NAME_ADAM);
|
||||
user.setEmail(USER_EMAIL);
|
||||
user.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user);
|
||||
|
||||
User user2 = new User();
|
||||
user2.setName(USER_NAME_PETER);
|
||||
user2.setEmail(USER_EMAIL2);
|
||||
user2.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user2);
|
||||
|
||||
User userByStatus = userRepository.findUserByStatusAndNameNamedParams(ACTIVE_STATUS, USER_NAME_ADAM);
|
||||
|
||||
assertThat(userByStatus.getName()).isEqualTo(USER_NAME_ADAM);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsersInDB_WhenFindUserByStatusAndNameWithQueryAnnotationNativeNamedParams_ThenReturnOneUser() {
|
||||
User user = new User();
|
||||
user.setName(USER_NAME_ADAM);
|
||||
user.setEmail(USER_EMAIL);
|
||||
user.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user);
|
||||
|
||||
User user2 = new User();
|
||||
user2.setName(USER_NAME_PETER);
|
||||
user2.setEmail(USER_EMAIL2);
|
||||
user2.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user2);
|
||||
|
||||
User userByStatus = userRepository.findUserByStatusAndNameNamedParamsNative(ACTIVE_STATUS, USER_NAME_ADAM);
|
||||
|
||||
assertThat(userByStatus.getName()).isEqualTo(USER_NAME_ADAM);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsersInDB_WhenFindUserByStatusAndNameWithQueryAnnotationNamedParamsCustomNames_ThenReturnOneUser() {
|
||||
User user = new User();
|
||||
user.setName(USER_NAME_ADAM);
|
||||
user.setEmail(USER_EMAIL);
|
||||
user.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user);
|
||||
|
||||
User user2 = new User();
|
||||
user2.setName(USER_NAME_PETER);
|
||||
user2.setEmail(USER_EMAIL2);
|
||||
user2.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user2);
|
||||
|
||||
User userByStatus = userRepository.findUserByUserStatusAndUserName(ACTIVE_STATUS, USER_NAME_ADAM);
|
||||
|
||||
assertThat(userByStatus.getName()).isEqualTo(USER_NAME_ADAM);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsersInDB_WhenFindUserByNameLikeWithQueryAnnotationIndexedParams_ThenReturnUser() {
|
||||
User user = new User();
|
||||
user.setName(USER_NAME_ADAM);
|
||||
user.setEmail(USER_EMAIL);
|
||||
user.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user);
|
||||
|
||||
User userByStatus = userRepository.findUserByNameLike("Ad");
|
||||
|
||||
assertThat(userByStatus.getName()).isEqualTo(USER_NAME_ADAM);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsersInDB_WhenFindUserByNameLikeWithQueryAnnotationNamedParams_ThenReturnUser() {
|
||||
User user = new User();
|
||||
user.setName(USER_NAME_ADAM);
|
||||
user.setEmail(USER_EMAIL);
|
||||
user.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user);
|
||||
|
||||
User userByStatus = userRepository.findUserByNameLikeNamedParam("Ad");
|
||||
|
||||
assertThat(userByStatus.getName()).isEqualTo(USER_NAME_ADAM);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsersInDB_WhenFindUserByNameLikeWithQueryAnnotationNative_ThenReturnUser() {
|
||||
User user = new User();
|
||||
user.setName(USER_NAME_ADAM);
|
||||
user.setEmail(USER_EMAIL);
|
||||
user.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user);
|
||||
|
||||
User userByStatus = userRepository.findUserByNameLikeNative("Ad");
|
||||
|
||||
assertThat(userByStatus.getName()).isEqualTo(USER_NAME_ADAM);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsersInDB_WhenFindAllWithSortByName_ThenReturnUsersSorted() {
|
||||
userRepository.save(new User(USER_NAME_ADAM, USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, INACTIVE_STATUS));
|
||||
|
||||
List<User> usersSortByName = userRepository.findAll(new Sort(Sort.Direction.ASC, "name"));
|
||||
|
||||
assertThat(usersSortByName.get(0)
|
||||
.getName()).isEqualTo(USER_NAME_ADAM);
|
||||
}
|
||||
|
||||
@Test(expected = PropertyReferenceException.class)
|
||||
public void givenUsersInDB_WhenFindAllSortWithFunction_ThenThrowException() {
|
||||
userRepository.save(new User(USER_NAME_ADAM, USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, INACTIVE_STATUS));
|
||||
|
||||
userRepository.findAll(new Sort(Sort.Direction.ASC, "name"));
|
||||
|
||||
List<User> usersSortByNameLength = userRepository.findAll(new Sort("LENGTH(name)"));
|
||||
|
||||
assertThat(usersSortByNameLength.get(0)
|
||||
.getName()).isEqualTo(USER_NAME_ADAM);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsersInDB_WhenFindAllSortWithFunctionQueryAnnotationJPQL_ThenReturnUsersSorted() {
|
||||
userRepository.save(new User(USER_NAME_ADAM, USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, INACTIVE_STATUS));
|
||||
|
||||
userRepository.findAllUsers(new Sort("name"));
|
||||
|
||||
List<User> usersSortByNameLength = userRepository.findAllUsers(JpaSort.unsafe("LENGTH(name)"));
|
||||
|
||||
assertThat(usersSortByNameLength.get(0)
|
||||
.getName()).isEqualTo(USER_NAME_ADAM);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsersInDB_WhenFindAllWithPageRequestQueryAnnotationJPQL_ThenReturnPageOfUsers() {
|
||||
userRepository.save(new User(USER_NAME_ADAM, USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", USER_EMAIL4, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE2", USER_EMAIL5, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", USER_EMAIL6, INACTIVE_STATUS));
|
||||
|
||||
Page<User> usersPage = userRepository.findAllUsersWithPagination(new PageRequest(1, 3));
|
||||
|
||||
assertThat(usersPage.getContent()
|
||||
.get(0)
|
||||
.getName()).isEqualTo("SAMPLE1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsersInDB_WhenFindAllWithPageRequestQueryAnnotationNative_ThenReturnPageOfUsers() {
|
||||
userRepository.save(new User(USER_NAME_ADAM, USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", USER_EMAIL4, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE2", USER_EMAIL5, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", USER_EMAIL6, INACTIVE_STATUS));
|
||||
|
||||
Page<User> usersSortByNameLength = userRepository.findAllUsersWithPaginationNative(new PageRequest(1, 3));
|
||||
|
||||
assertThat(usersSortByNameLength.getContent()
|
||||
.get(0)
|
||||
.getName()).isEqualTo("SAMPLE1");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenUsersInDB_WhenUpdateStatusForNameModifyingQueryAnnotationJPQL_ThenModifyMatchingUsers() {
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", USER_EMAIL4, ACTIVE_STATUS));
|
||||
|
||||
int updatedUsersSize = userRepository.updateUserSetStatusForName(INACTIVE_STATUS, "SAMPLE");
|
||||
|
||||
assertThat(updatedUsersSize).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsersInDB_WhenFindByEmailsWithDynamicQuery_ThenReturnCollection() {
|
||||
|
||||
User user1 = new User();
|
||||
user1.setEmail(USER_EMAIL);
|
||||
userRepository.save(user1);
|
||||
|
||||
User user2 = new User();
|
||||
user2.setEmail(USER_EMAIL2);
|
||||
userRepository.save(user2);
|
||||
|
||||
User user3 = new User();
|
||||
user3.setEmail(USER_EMAIL3);
|
||||
userRepository.save(user3);
|
||||
|
||||
Set<String> emails = new HashSet<>();
|
||||
emails.add(USER_EMAIL2);
|
||||
emails.add(USER_EMAIL3);
|
||||
|
||||
Collection<User> usersWithEmails = userRepository.findUserByEmails(emails);
|
||||
|
||||
assertThat(usersWithEmails.size()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsersInDBWhenFindByNameListReturnCollection() {
|
||||
|
||||
User user1 = new User();
|
||||
user1.setName(USER_NAME_ADAM);
|
||||
user1.setEmail(USER_EMAIL);
|
||||
userRepository.save(user1);
|
||||
|
||||
User user2 = new User();
|
||||
user2.setName(USER_NAME_PETER);
|
||||
user2.setEmail(USER_EMAIL2);
|
||||
userRepository.save(user2);
|
||||
|
||||
List<String> names = Arrays.asList(USER_NAME_ADAM, USER_NAME_PETER);
|
||||
|
||||
List<User> usersWithNames = userRepository.findUserByNameList(names);
|
||||
|
||||
assertThat(usersWithNames.size()).isEqualTo(2);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void whenInsertedWithQuery_ThenUserIsPersisted() {
|
||||
userRepository.insertUser(USER_NAME_ADAM, 1, ACTIVE_STATUS, USER_EMAIL);
|
||||
userRepository.insertUser(USER_NAME_PETER, 1, ACTIVE_STATUS, USER_EMAIL2);
|
||||
|
||||
User userAdam = userRepository.findUserByNameLike(USER_NAME_ADAM);
|
||||
User userPeter = userRepository.findUserByNameLike(USER_NAME_PETER);
|
||||
|
||||
assertThat(userAdam).isNotNull();
|
||||
assertThat(userAdam.getEmail()).isEqualTo(USER_EMAIL);
|
||||
assertThat(userPeter).isNotNull();
|
||||
assertThat(userPeter.getEmail()).isEqualTo(USER_EMAIL2);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUp() {
|
||||
userRepository.deleteAll();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.baeldung.boot.daos;
|
||||
|
||||
import com.baeldung.boot.Application;
|
||||
import com.baeldung.boot.config.PersistenceConfiguration;
|
||||
import com.baeldung.boot.domain.User;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Created by adam.
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class)
|
||||
@DirtiesContext
|
||||
public class UserRepositoryIntegrationTest extends UserRepositoryCommon {
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenUsersInDBWhenUpdateStatusForNameModifyingQueryAnnotationNativeThenModifyMatchingUsers() {
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", USER_EMAIL4, ACTIVE_STATUS));
|
||||
userRepository.flush();
|
||||
|
||||
int updatedUsersSize = userRepository.updateUserSetStatusForNameNative(INACTIVE_STATUS, "SAMPLE");
|
||||
|
||||
assertThat(updatedUsersSize).isEqualTo(2);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.boot.daos;
|
||||
|
||||
import com.baeldung.boot.domain.User;
|
||||
import com.baeldung.util.BaeldungPostgresqlContainer;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.testcontainers.containers.PostgreSQLContainer;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Created by adam.
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@ActiveProfiles({"tc", "tc-auto"})
|
||||
public class UserRepositoryTCAutoIntegrationTest extends UserRepositoryCommon {
|
||||
|
||||
@ClassRule
|
||||
public static PostgreSQLContainer postgreSQLContainer = BaeldungPostgresqlContainer.getInstance();
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenUsersInDB_WhenUpdateStatusForNameModifyingQueryAnnotationNativePostgres_ThenModifyMatchingUsers() {
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", USER_EMAIL4, ACTIVE_STATUS));
|
||||
userRepository.flush();
|
||||
|
||||
int updatedUsersSize = userRepository.updateUserSetStatusForNameNativePostgres(INACTIVE_STATUS, "SAMPLE");
|
||||
|
||||
assertThat(updatedUsersSize).isEqualTo(2);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package com.baeldung.boot.daos;
|
||||
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.testcontainers.containers.PostgreSQLContainer;
|
||||
|
||||
import com.baeldung.boot.domain.User;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("tc")
|
||||
@ContextConfiguration(initializers = {UserRepositoryTCIntegrationTest.Initializer.class})
|
||||
public class UserRepositoryTCIntegrationTest extends UserRepositoryCommon {
|
||||
|
||||
@ClassRule
|
||||
public static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer("postgres:11.1")
|
||||
.withDatabaseName("integration-tests-db")
|
||||
.withUsername("sa")
|
||||
.withPassword("sa");
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenUsersInDB_WhenUpdateStatusForNameModifyingQueryAnnotationNative_ThenModifyMatchingUsers() {
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", USER_EMAIL4, ACTIVE_STATUS));
|
||||
userRepository.flush();
|
||||
|
||||
int updatedUsersSize = userRepository.updateUserSetStatusForNameNativePostgres(INACTIVE_STATUS, "SAMPLE");
|
||||
|
||||
assertThat(updatedUsersSize).isEqualTo(2);
|
||||
}
|
||||
|
||||
static class Initializer
|
||||
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
|
||||
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
|
||||
TestPropertyValues.of(
|
||||
"spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(),
|
||||
"spring.datasource.username=" + postgreSQLContainer.getUsername(),
|
||||
"spring.datasource.password=" + postgreSQLContainer.getPassword()
|
||||
).applyTo(configurableApplicationContext.getEnvironment());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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.Mockito.times;
|
||||
|
@ -15,6 +15,10 @@ import org.springframework.boot.test.context.SpringBootTest;
|
|||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
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
|
||||
@SpringBootTest
|
||||
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.Mockito.times;
|
||||
|
@ -14,6 +14,10 @@ import org.springframework.boot.test.context.SpringBootTest;
|
|||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
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
|
||||
@SpringBootTest
|
||||
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.Mockito.times;
|
||||
|
@ -16,6 +16,11 @@ import org.springframework.context.ApplicationEventPublisher;
|
|||
import org.springframework.context.annotation.Bean;
|
||||
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
|
||||
@SpringBootTest
|
||||
class AggregateEventsIntegrationTest {
|
|
@ -1,10 +1,12 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.ddd.event;
|
||||
package com.baeldung.boot.ddd.event;
|
||||
|
||||
import org.springframework.transaction.event.TransactionalEventListener;
|
||||
|
||||
import com.baeldung.boot.ddd.event.DomainEvent;
|
||||
|
||||
interface TestEventHandler {
|
||||
@TransactionalEventListener
|
||||
void handleEvent(DomainEvent event);
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.passenger;
|
||||
package com.baeldung.boot.passenger;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
|
@ -24,6 +24,9 @@ import org.springframework.data.domain.PageRequest;
|
|||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.boot.passenger.Passenger;
|
||||
import com.baeldung.boot.passenger.PassengerRepository;
|
||||
|
||||
@DataJpaTest
|
||||
@RunWith(SpringRunner.class)
|
||||
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 org.hamcrest.Matchers;
|
||||
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.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
@ -16,7 +21,7 @@ import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
|||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = {PersistenceConfiguration.class}, loader = AnnotationConfigContextLoader.class)
|
||||
@SpringBootTest(classes=Application.class)
|
||||
public class FooServicePersistenceIntegrationTest extends AbstractServicePersistenceIntegrationTest<Foo> {
|
||||
|
||||
@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.assertTrue;
|
||||
|
@ -16,16 +16,19 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.config.PersistenceConfiguration;
|
||||
import com.baeldung.domain.Bar;
|
||||
import com.baeldung.boot.Application;
|
||||
import com.baeldung.boot.config.PersistenceConfiguration;
|
||||
import com.baeldung.boot.domain.Bar;
|
||||
import com.baeldung.boot.services.IBarService;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceConfiguration.class }, loader = AnnotationConfigContextLoader.class)
|
||||
@SpringBootTest(classes=Application.class)
|
||||
public class SpringDataJPABarAuditIntegrationTest {
|
||||
|
||||
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.assertTrue;
|
||||
|
@ -10,6 +10,7 @@ import java.util.Optional;
|
|||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
@ -17,17 +18,17 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.baeldung.config.PersistenceProductConfiguration;
|
||||
import com.baeldung.config.PersistenceUserConfiguration;
|
||||
import com.baeldung.dao.repositories.product.ProductRepository;
|
||||
import com.baeldung.dao.repositories.user.PossessionRepository;
|
||||
import com.baeldung.dao.repositories.user.UserRepository;
|
||||
import com.baeldung.domain.product.Product;
|
||||
import com.baeldung.domain.user.Possession;
|
||||
import com.baeldung.domain.user.User;
|
||||
import com.baeldung.boot.domain.Possession;
|
||||
import com.baeldung.boot.domain.User;
|
||||
import com.baeldung.boot.user.PossessionRepository;
|
||||
import com.baeldung.boot.user.UserRepository;
|
||||
import com.baeldung.multipledb.PersistenceProductConfiguration;
|
||||
import com.baeldung.multipledb.PersistenceUserConfiguration;
|
||||
import com.baeldung.multipledb.Product;
|
||||
import com.baeldung.multipledb.ProductRepository;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceUserConfiguration.class, PersistenceProductConfiguration.class })
|
||||
@SpringBootTest(classes=MultipleDbApplication.class)
|
||||
@EnableTransactionManagement
|
||||
@DirtiesContext
|
||||
public class JpaMultipleDBIntegrationTest {
|
|
@ -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.hasSize;
|
||||
|
@ -13,6 +13,7 @@ import org.junit.Before;
|
|||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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.PageRequest;
|
||||
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.Transactional;
|
||||
|
||||
import com.baeldung.config.PersistenceProductConfiguration;
|
||||
import com.baeldung.domain.product.Product;
|
||||
import com.baeldung.multipledb.PersistenceProductConfiguration;
|
||||
import com.baeldung.multipledb.Product;
|
||||
import com.baeldung.multipledb.ProductRepository;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceProductConfiguration.class })
|
||||
@SpringBootTest(classes=MultipleDbApplication.class)
|
||||
@EnableTransactionManagement
|
||||
public class ProductRepositoryIntegrationTest {
|
||||
|
|
@ -5,7 +5,7 @@ import org.junit.runner.RunWith;
|
|||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.Application;
|
||||
import com.baeldung.boot.Application;
|
||||
|
||||
@RunWith(SpringRunner.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.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.Application;
|
||||
import com.baeldung.config.PersistenceConfiguration;
|
||||
import com.baeldung.config.PersistenceProductConfiguration;
|
||||
import com.baeldung.config.PersistenceUserConfiguration;
|
||||
import com.baeldung.boot.Application;
|
||||
import com.baeldung.boot.config.PersistenceConfiguration;
|
||||
import com.baeldung.multipledb.PersistenceProductConfiguration;
|
||||
import com.baeldung.multipledb.PersistenceUserConfiguration;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@DataJpaTest(excludeAutoConfiguration = {
|
||||
|
|
Loading…
Reference in New Issue