[BAEL-2048] Rearranging code
This commit is contained in:
parent
2e6bc30aad
commit
7ddb86e748
@ -21,8 +21,8 @@
|
|||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>${maven-compiler-plugin.version}</version>
|
<version>${maven-compiler-plugin.version}</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>${maven.compiler.source.version}</source>
|
<source>10</source>
|
||||||
<target>${maven.compiler.target.version}</target>
|
<target>10</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
|
@ -1,14 +1,17 @@
|
|||||||
package com.baeldung;
|
package com.baeldung;
|
||||||
|
|
||||||
|
import com.baeldung.dao.repositories.impl.ExtendedRepositoryImpl;
|
||||||
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.context.ApplicationContext;
|
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||||
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@EnableJpaRepositories(repositoryBaseClass = ExtendedRepositoryImpl.class)
|
||||||
public class Application {
|
public class Application {
|
||||||
private static ApplicationContext applicationContext;
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
applicationContext = SpringApplication.run(Application.class, args);
|
SpringApplication.run(Application.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,20 @@
|
|||||||
package org.baeldung.spring;
|
package com.baeldung.config;
|
||||||
|
|
||||||
|
import com.baeldung.services.IBarService;
|
||||||
|
import com.baeldung.services.impl.BarSpringDataJpaService;
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
|
import com.baeldung.dao.repositories.impl.ExtendedRepositoryImpl;
|
||||||
|
import com.baeldung.services.IFooService;
|
||||||
|
import com.baeldung.services.impl.FooService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.*;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.context.annotation.PropertySource;
|
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
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.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||||
|
import org.springframework.orm.jpa.JpaVendorAdapter;
|
||||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||||
import org.springframework.transaction.PlatformTransactionManager;
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
@ -20,32 +24,31 @@ import javax.sql.DataSource;
|
|||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
|
@ComponentScan({"com.baeldung.dao", "com.baeldung.services"})
|
||||||
@EnableTransactionManagement
|
@EnableTransactionManagement
|
||||||
@PropertySource({ "classpath:persistence-${envTarget:h2}.properties" })
|
@EnableJpaRepositories(basePackages = {"com.baeldung.dao"}, repositoryBaseClass = ExtendedRepositoryImpl.class)
|
||||||
@ComponentScan({ "org.baeldung.persistence" })
|
@EnableJpaAuditing
|
||||||
// @ImportResource("classpath*:springDataPersistenceConfig.xml")
|
@PropertySource("classpath:persistence.properties")
|
||||||
@EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao")
|
public class PersistenceConfiguration {
|
||||||
public class PersistenceConfig {
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private Environment env;
|
private Environment env;
|
||||||
|
|
||||||
public PersistenceConfig() {
|
public PersistenceConfiguration() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
||||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
final LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
|
||||||
em.setDataSource(dataSource());
|
emf.setDataSource(dataSource());
|
||||||
em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
|
emf.setPackagesToScan("com.baeldung.domain");
|
||||||
|
|
||||||
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||||
// vendorAdapter.set
|
emf.setJpaVendorAdapter(vendorAdapter);
|
||||||
em.setJpaVendorAdapter(vendorAdapter);
|
emf.setJpaProperties(hibernateProperties());
|
||||||
em.setJpaProperties(additionalProperties());
|
|
||||||
|
|
||||||
return em;
|
return emf;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ -63,7 +66,6 @@ public class PersistenceConfig {
|
|||||||
public PlatformTransactionManager transactionManager() {
|
public PlatformTransactionManager transactionManager() {
|
||||||
final JpaTransactionManager transactionManager = new JpaTransactionManager();
|
final JpaTransactionManager transactionManager = new JpaTransactionManager();
|
||||||
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
|
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
|
||||||
|
|
||||||
return transactionManager;
|
return transactionManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,11 +74,28 @@ public class PersistenceConfig {
|
|||||||
return new PersistenceExceptionTranslationPostProcessor();
|
return new PersistenceExceptionTranslationPostProcessor();
|
||||||
}
|
}
|
||||||
|
|
||||||
final Properties additionalProperties() {
|
@Bean
|
||||||
|
public IBarService barSpringDataJpaService() {
|
||||||
|
return new BarSpringDataJpaService();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public IFooService fooService() {
|
||||||
|
return new FooService();
|
||||||
|
}
|
||||||
|
|
||||||
|
private final Properties hibernateProperties() {
|
||||||
final Properties hibernateProperties = new Properties();
|
final Properties hibernateProperties = new Properties();
|
||||||
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||||
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
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");
|
// 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;
|
return hibernateProperties;
|
||||||
}
|
}
|
||||||
|
|
@ -1,9 +1,6 @@
|
|||||||
package org.baeldung.config;
|
package com.baeldung.config;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
|
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
@ -16,16 +13,17 @@ import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
|||||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||||
import org.springframework.transaction.PlatformTransactionManager;
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
import javax.sql.DataSource;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@PropertySource({ "classpath:persistence-multiple-db.properties" })
|
@PropertySource({"classpath:persistence-multiple-db.properties"})
|
||||||
@EnableJpaRepositories(basePackages = "org.baeldung.persistence.multiple.dao.product", entityManagerFactoryRef = "productEntityManager", transactionManagerRef = "productTransactionManager")
|
@EnableJpaRepositories(basePackages = "com.baeldung.dao.repositories.product", entityManagerFactoryRef = "productEntityManager", transactionManagerRef = "productTransactionManager")
|
||||||
public class ProductConfig {
|
public class PersistenceProductConfiguration {
|
||||||
@Autowired
|
@Autowired
|
||||||
private Environment env;
|
private Environment env;
|
||||||
|
|
||||||
public ProductConfig() {
|
public PersistenceProductConfiguration() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,7 +33,7 @@ public class ProductConfig {
|
|||||||
public LocalContainerEntityManagerFactoryBean productEntityManager() {
|
public LocalContainerEntityManagerFactoryBean productEntityManager() {
|
||||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||||
em.setDataSource(productDataSource());
|
em.setDataSource(productDataSource());
|
||||||
em.setPackagesToScan(new String[] { "org.baeldung.persistence.multiple.model.product" });
|
em.setPackagesToScan("com.baeldung.domain.product");
|
||||||
|
|
||||||
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||||
em.setJpaVendorAdapter(vendorAdapter);
|
em.setJpaVendorAdapter(vendorAdapter);
|
@ -1,9 +1,6 @@
|
|||||||
package org.baeldung.config;
|
package com.baeldung.config;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
|
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
@ -17,16 +14,17 @@ import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
|||||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||||
import org.springframework.transaction.PlatformTransactionManager;
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
import javax.sql.DataSource;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@PropertySource({ "classpath:persistence-multiple-db.properties" })
|
@PropertySource({"classpath:persistence-multiple-db.properties"})
|
||||||
@EnableJpaRepositories(basePackages = "org.baeldung.persistence.multiple.dao.user", entityManagerFactoryRef = "userEntityManager", transactionManagerRef = "userTransactionManager")
|
@EnableJpaRepositories(basePackages = "com.baeldung.dao.repositories.user", entityManagerFactoryRef = "userEntityManager", transactionManagerRef = "userTransactionManager")
|
||||||
public class UserConfig {
|
public class PersistenceUserConfiguration {
|
||||||
@Autowired
|
@Autowired
|
||||||
private Environment env;
|
private Environment env;
|
||||||
|
|
||||||
public UserConfig() {
|
public PersistenceUserConfiguration() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,7 +35,7 @@ public class UserConfig {
|
|||||||
public LocalContainerEntityManagerFactoryBean userEntityManager() {
|
public LocalContainerEntityManagerFactoryBean userEntityManager() {
|
||||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||||
em.setDataSource(userDataSource());
|
em.setDataSource(userDataSource());
|
||||||
em.setPackagesToScan(new String[] { "org.baeldung.persistence.multiple.model.user" });
|
em.setPackagesToScan("com.baeldung.domain.user");
|
||||||
|
|
||||||
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||||
em.setJpaVendorAdapter(vendorAdapter);
|
em.setJpaVendorAdapter(vendorAdapter);
|
@ -1,6 +1,6 @@
|
|||||||
package org.baeldung.persistence.dao;
|
package com.baeldung.dao;
|
||||||
|
|
||||||
import org.baeldung.persistence.model.Foo;
|
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;
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.repository;
|
package com.baeldung.dao.repositories;
|
||||||
|
|
||||||
import com.baeldung.domain.Article;
|
import com.baeldung.domain.Article;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.repository;
|
package com.baeldung.dao.repositories;
|
||||||
|
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.repository;
|
package com.baeldung.dao.repositories;
|
||||||
|
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.extended.persistence.dao;
|
package com.baeldung.dao.repositories;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -8,5 +8,7 @@ import org.springframework.data.repository.NoRepositoryBean;
|
|||||||
|
|
||||||
@NoRepositoryBean
|
@NoRepositoryBean
|
||||||
public interface ExtendedRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
|
public interface ExtendedRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
|
||||||
public List<T> findByAttributeContainsText(String attributeName, String text);
|
|
||||||
|
List<T> findByAttributeContainsText(String attributeName, String text);
|
||||||
|
|
||||||
}
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
package com.baeldung.dao.repositories;
|
||||||
|
|
||||||
|
import com.baeldung.domain.Student;
|
||||||
|
|
||||||
|
public interface ExtendedStudentRepository extends ExtendedRepository<Student, Long> {
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
package com.baeldung.persistence.dao;
|
package com.baeldung.dao.repositories;
|
||||||
|
|
||||||
import com.baeldung.persistence.model.Bar;
|
import com.baeldung.domain.Bar;
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.repository;
|
package com.baeldung.dao.repositories;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.repository;
|
package com.baeldung.dao.repositories;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.repository;
|
package com.baeldung.dao.repositories;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.repository;
|
package com.baeldung.dao.repositories;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.repository.impl;
|
package com.baeldung.dao.repositories.impl;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
|
|
||||||
@ -6,7 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import com.baeldung.domain.Item;
|
import com.baeldung.domain.Item;
|
||||||
import com.baeldung.repository.CustomItemRepository;
|
import com.baeldung.dao.repositories.CustomItemRepository;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public class CustomItemRepositoryImpl implements CustomItemRepository {
|
public class CustomItemRepositoryImpl implements CustomItemRepository {
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.repository.impl;
|
package com.baeldung.dao.repositories.impl;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
|
|
||||||
@ -8,7 +8,7 @@ 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.domain.ItemType;
|
||||||
import com.baeldung.repository.CustomItemTypeRepository;
|
import com.baeldung.dao.repositories.CustomItemTypeRepository;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public class CustomItemTypeRepositoryImpl implements CustomItemTypeRepository {
|
public class CustomItemTypeRepositoryImpl implements CustomItemTypeRepository {
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.extended.persistence.dao;
|
package com.baeldung.dao.repositories.impl;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -10,6 +10,7 @@ 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;
|
||||||
|
|
@ -1,6 +1,6 @@
|
|||||||
package org.baeldung.persistence.multiple.dao.product;
|
package com.baeldung.dao.repositories.product;
|
||||||
|
|
||||||
import org.baeldung.persistence.multiple.model.product.Product;
|
import com.baeldung.domain.product.Product;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
public interface ProductRepository extends JpaRepository<Product, Integer> {
|
public interface ProductRepository extends JpaRepository<Product, Integer> {
|
@ -1,6 +1,6 @@
|
|||||||
package org.baeldung.persistence.multiple.dao.user;
|
package com.baeldung.dao.repositories.user;
|
||||||
|
|
||||||
import org.baeldung.persistence.multiple.model.user.Possession;
|
import com.baeldung.domain.user.Possession;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
public interface PossessionRepository extends JpaRepository<Possession, Long> {
|
public interface PossessionRepository extends JpaRepository<Possession, Long> {
|
@ -1,6 +1,6 @@
|
|||||||
package org.baeldung.persistence.repository;
|
package com.baeldung.dao.repositories.user;
|
||||||
|
|
||||||
import org.baeldung.persistence.model.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;
|
||||||
@ -8,24 +8,25 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
|||||||
import org.springframework.data.jpa.repository.Modifying;
|
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 org.springframework.stereotype.Repository;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@Repository("userRepository")
|
|
||||||
public interface UserRepository extends JpaRepository<User, Integer> {
|
public interface UserRepository extends JpaRepository<User, Integer> {
|
||||||
|
|
||||||
|
Stream<User> findAllByName(String name);
|
||||||
|
|
||||||
@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(value = "SELECT * FROM USERS u WHERE u.status = 1", nativeQuery = true)
|
@Query(value = "SELECT * FROM USERS.USERS u WHERE u.status = 1", nativeQuery = true)
|
||||||
Collection<User> findAllActiveUsersNative();
|
Collection<User> findAllActiveUsersNative();
|
||||||
|
|
||||||
@Query("SELECT u FROM User u WHERE u.status = ?1")
|
@Query("SELECT u FROM User u WHERE u.status = ?1")
|
||||||
User findUserByStatus(Integer status);
|
User findUserByStatus(Integer status);
|
||||||
|
|
||||||
@Query(value = "SELECT * FROM Users u WHERE u.status = ?1", nativeQuery = true)
|
@Query(value = "SELECT * FROM USERS.Users u WHERE u.status = ?1", nativeQuery = true)
|
||||||
User findUserByStatusNative(Integer status);
|
User findUserByStatusNative(Integer status);
|
||||||
|
|
||||||
@Query("SELECT u FROM User u WHERE u.status = ?1 and u.name = ?2")
|
@Query("SELECT u FROM User u WHERE u.status = ?1 and u.name = ?2")
|
||||||
@ -34,7 +35,7 @@ public interface UserRepository extends JpaRepository<User, Integer> {
|
|||||||
@Query("SELECT u FROM User u WHERE u.status = :status and u.name = :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);
|
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)
|
@Query(value = "SELECT * FROM USERS.Users u WHERE u.status = :status AND u.name = :name", nativeQuery = true)
|
||||||
User findUserByStatusAndNameNamedParamsNative(@Param("status") Integer status, @Param("name") String name);
|
User findUserByStatusAndNameNamedParamsNative(@Param("status") Integer status, @Param("name") String name);
|
||||||
|
|
||||||
@Query("SELECT u FROM User u WHERE u.status = :status and u.name = :name")
|
@Query("SELECT u FROM User u WHERE u.status = :status and u.name = :name")
|
||||||
@ -46,7 +47,7 @@ public interface UserRepository extends JpaRepository<User, Integer> {
|
|||||||
@Query("SELECT u FROM User u WHERE u.name like :name%")
|
@Query("SELECT u FROM User u WHERE u.name like :name%")
|
||||||
User findUserByNameLikeNamedParam(@Param("name") String name);
|
User findUserByNameLikeNamedParam(@Param("name") String name);
|
||||||
|
|
||||||
@Query(value = "SELECT * FROM users u WHERE u.name LIKE ?1%", nativeQuery = true)
|
@Query(value = "SELECT * FROM USERS.users u WHERE u.name LIKE ?1%", nativeQuery = true)
|
||||||
User findUserByNameLikeNative(String name);
|
User findUserByNameLikeNative(String name);
|
||||||
|
|
||||||
@Query(value = "SELECT u FROM User u")
|
@Query(value = "SELECT u FROM User u")
|
||||||
@ -55,7 +56,7 @@ public interface UserRepository extends JpaRepository<User, Integer> {
|
|||||||
@Query(value = "SELECT u FROM User u ORDER BY id")
|
@Query(value = "SELECT u FROM User u ORDER BY id")
|
||||||
Page<User> findAllUsersWithPagination(Pageable pageable);
|
Page<User> findAllUsersWithPagination(Pageable pageable);
|
||||||
|
|
||||||
@Query(value = "SELECT * FROM Users ORDER BY id", countQuery = "SELECT count(*) FROM Users", nativeQuery = true)
|
@Query(value = "SELECT * FROM USERS.Users ORDER BY id", countQuery = "SELECT count(*) FROM USERS.Users", nativeQuery = true)
|
||||||
Page<User> findAllUsersWithPaginationNative(Pageable pageable);
|
Page<User> findAllUsersWithPaginationNative(Pageable pageable);
|
||||||
|
|
||||||
@Modifying
|
@Modifying
|
||||||
@ -63,7 +64,6 @@ public interface UserRepository extends JpaRepository<User, Integer> {
|
|||||||
int updateUserSetStatusForName(@Param("status") Integer status, @Param("name") String name);
|
int updateUserSetStatusForName(@Param("status") Integer status, @Param("name") String name);
|
||||||
|
|
||||||
@Modifying
|
@Modifying
|
||||||
@Query(value = "UPDATE Users u SET u.status = ? WHERE u.name = ?", nativeQuery = true)
|
@Query(value = "UPDATE USERS.Users u SET u.status = ? WHERE u.name = ?", nativeQuery = true)
|
||||||
int updateUserSetStatusForNameNative(Integer status, String name);
|
int updateUserSetStatusForNameNative(Integer status, String name);
|
||||||
|
|
||||||
}
|
}
|
@ -1,7 +1,6 @@
|
|||||||
package com.baeldung.persistence.model;
|
package com.baeldung.domain;
|
||||||
|
|
||||||
import com.google.common.collect.Sets;
|
import com.google.common.collect.Sets;
|
||||||
import org.baeldung.persistence.model.Foo;
|
|
||||||
import org.hibernate.annotations.OrderBy;
|
import org.hibernate.annotations.OrderBy;
|
||||||
import org.hibernate.envers.Audited;
|
import org.hibernate.envers.Audited;
|
||||||
import org.jboss.logging.Logger;
|
import org.jboss.logging.Logger;
|
||||||
@ -23,31 +22,6 @@ import java.util.Set;
|
|||||||
public class Bar implements Serializable {
|
public class Bar implements Serializable {
|
||||||
|
|
||||||
private static Logger logger = Logger.getLogger(Bar.class);
|
private static Logger logger = Logger.getLogger(Bar.class);
|
||||||
|
|
||||||
public enum OPERATION {
|
|
||||||
INSERT, UPDATE, DELETE;
|
|
||||||
private String value;
|
|
||||||
|
|
||||||
OPERATION() {
|
|
||||||
value = toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getValue() {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static OPERATION parse(final String value) {
|
|
||||||
OPERATION operation = null;
|
|
||||||
for (final OPERATION op : OPERATION.values()) {
|
|
||||||
if (op.getValue().equals(value)) {
|
|
||||||
operation = op;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return operation;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
@Column(name = "id")
|
@Column(name = "id")
|
||||||
@ -55,30 +29,23 @@ public class Bar implements Serializable {
|
|||||||
|
|
||||||
@Column(name = "name")
|
@Column(name = "name")
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "bar", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
@OneToMany(mappedBy = "bar", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||||
@OrderBy(clause = "NAME DESC")
|
@OrderBy(clause = "NAME DESC")
|
||||||
// @NotAudited
|
// @NotAudited
|
||||||
private Set<Foo> fooSet = Sets.newHashSet();
|
private Set<Foo> fooSet = Sets.newHashSet();
|
||||||
|
|
||||||
@Column(name = "operation")
|
@Column(name = "operation")
|
||||||
private String operation;
|
private String operation;
|
||||||
|
|
||||||
@Column(name = "timestamp")
|
@Column(name = "timestamp")
|
||||||
private long timestamp;
|
private long timestamp;
|
||||||
|
|
||||||
@Column(name = "created_date", updatable = false, nullable = false)
|
@Column(name = "created_date", updatable = false, nullable = false)
|
||||||
@CreatedDate
|
@CreatedDate
|
||||||
private long createdDate;
|
private long createdDate;
|
||||||
|
|
||||||
@Column(name = "modified_date")
|
@Column(name = "modified_date")
|
||||||
@LastModifiedDate
|
@LastModifiedDate
|
||||||
private long modifiedDate;
|
private long modifiedDate;
|
||||||
|
|
||||||
@Column(name = "created_by")
|
@Column(name = "created_by")
|
||||||
@CreatedBy
|
@CreatedBy
|
||||||
private String createdBy;
|
private String createdBy;
|
||||||
|
|
||||||
@Column(name = "modified_by")
|
@Column(name = "modified_by")
|
||||||
@LastModifiedBy
|
@LastModifiedBy
|
||||||
private String modifiedBy;
|
private String modifiedBy;
|
||||||
@ -93,12 +60,12 @@ public class Bar implements Serializable {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
// API
|
|
||||||
|
|
||||||
public Set<Foo> getFooSet() {
|
public Set<Foo> getFooSet() {
|
||||||
return fooSet;
|
return fooSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// API
|
||||||
|
|
||||||
public void setFooSet(final Set<Foo> fooSet) {
|
public void setFooSet(final Set<Foo> fooSet) {
|
||||||
this.fooSet = fooSet;
|
this.fooSet = fooSet;
|
||||||
}
|
}
|
||||||
@ -123,6 +90,10 @@ public class Bar implements Serializable {
|
|||||||
return OPERATION.parse(operation);
|
return OPERATION.parse(operation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setOperation(final String operation) {
|
||||||
|
this.operation = operation;
|
||||||
|
}
|
||||||
|
|
||||||
public void setOperation(final OPERATION operation) {
|
public void setOperation(final OPERATION operation) {
|
||||||
this.operation = operation.getValue();
|
this.operation = operation.getValue();
|
||||||
}
|
}
|
||||||
@ -167,10 +138,6 @@ public class Bar implements Serializable {
|
|||||||
this.modifiedBy = modifiedBy;
|
this.modifiedBy = modifiedBy;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setOperation(final String operation) {
|
|
||||||
this.operation = operation;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
final int prime = 31;
|
final int prime = 31;
|
||||||
@ -226,4 +193,28 @@ public class Bar implements Serializable {
|
|||||||
setTimestamp((new Date()).getTime());
|
setTimestamp((new Date()).getTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum OPERATION {
|
||||||
|
INSERT, UPDATE, DELETE;
|
||||||
|
private String value;
|
||||||
|
|
||||||
|
OPERATION() {
|
||||||
|
value = toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static OPERATION parse(final String value) {
|
||||||
|
OPERATION operation = null;
|
||||||
|
for (final OPERATION op : OPERATION.values()) {
|
||||||
|
if (op.getValue().equals(value)) {
|
||||||
|
operation = op;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return operation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,11 +1,11 @@
|
|||||||
package com.baeldung.persistence.model;
|
package com.baeldung.domain;
|
||||||
|
|
||||||
import org.hibernate.envers.Audited;
|
import org.hibernate.envers.Audited;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
@NamedNativeQueries({ @NamedNativeQuery(name = "callGetAllFoos", query = "CALL GetAllFoos()", resultClass = Foo.class), @NamedNativeQuery(name = "callGetFoosByName", query = "CALL GetFoosByName(:fooName)", resultClass = Foo.class) })
|
@NamedNativeQueries({@NamedNativeQuery(name = "callGetAllFoos", query = "CALL GetAllFoos()", resultClass = Foo.class), @NamedNativeQuery(name = "callGetFoosByName", query = "CALL GetFoosByName(:fooName)", resultClass = Foo.class)})
|
||||||
@Entity
|
@Entity
|
||||||
@Audited
|
@Audited
|
||||||
// @Proxy(lazy = false)
|
// @Proxy(lazy = false)
|
||||||
@ -16,7 +16,7 @@ public class Foo implements Serializable {
|
|||||||
@Column(name = "id")
|
@Column(name = "id")
|
||||||
private long id;
|
private long id;
|
||||||
|
|
||||||
@Column(name = "name")
|
@Column(name = "name", nullable = false)
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@ManyToOne(targetEntity = Bar.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
@ManyToOne(targetEntity = Bar.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.inmemory.persistence.model;
|
package com.baeldung.domain;
|
||||||
|
|
||||||
import javax.persistence.Embeddable;
|
import javax.persistence.Embeddable;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.inmemory.persistence.model;
|
package com.baeldung.domain;
|
||||||
|
|
||||||
import javax.persistence.Embeddable;
|
import javax.persistence.Embeddable;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.inmemory.persistence.model;
|
package com.baeldung.domain;
|
||||||
|
|
||||||
import javax.persistence.ElementCollection;
|
import javax.persistence.ElementCollection;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
@ -1,11 +1,11 @@
|
|||||||
package org.baeldung.persistence.multiple.model.product;
|
package com.baeldung.domain.product;
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(schema = "spring_jpa_product")
|
@Table(schema = "products")
|
||||||
public class Product {
|
public class Product {
|
||||||
|
|
||||||
@Id
|
@Id
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.persistence.multiple.model.user;
|
package com.baeldung.domain.user;
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
@ -7,7 +7,7 @@ import javax.persistence.Id;
|
|||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(schema = "spring_jpa_user")
|
@Table(schema = "users")
|
||||||
public class Possession {
|
public class Possession {
|
||||||
|
|
||||||
@Id
|
@Id
|
@ -1,30 +1,20 @@
|
|||||||
package org.baeldung.persistence.multiple.model.user;
|
package com.baeldung.domain.user;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.persistence.Column;
|
|
||||||
import javax.persistence.Entity;
|
|
||||||
import javax.persistence.GeneratedValue;
|
|
||||||
import javax.persistence.GenerationType;
|
|
||||||
import javax.persistence.Id;
|
|
||||||
import javax.persistence.OneToMany;
|
|
||||||
import javax.persistence.Table;
|
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(schema = "spring_jpa_user")
|
@Table(name = "users", schema = "users")
|
||||||
public class User {
|
public class User {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
private int age;
|
||||||
@Column(unique = true, nullable = false)
|
@Column(unique = true, nullable = false)
|
||||||
private String email;
|
private String email;
|
||||||
|
private Integer status;
|
||||||
private int age;
|
|
||||||
|
|
||||||
@OneToMany
|
@OneToMany
|
||||||
List<Possession> possessionList;
|
List<Possession> possessionList;
|
||||||
|
|
||||||
@ -32,6 +22,12 @@ public class User {
|
|||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public User(String name, String email, Integer status) {
|
||||||
|
this.name = name;
|
||||||
|
this.email = email;
|
||||||
|
this.status = status;
|
||||||
|
}
|
||||||
|
|
||||||
public int getId() {
|
public int getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
@ -56,6 +52,14 @@ public class User {
|
|||||||
this.email = email;
|
this.email = email;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Integer getStatus() {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStatus(Integer status) {
|
||||||
|
this.status = status;
|
||||||
|
}
|
||||||
|
|
||||||
public int getAge() {
|
public int getAge() {
|
||||||
return age;
|
return age;
|
||||||
}
|
}
|
||||||
@ -78,4 +82,5 @@ public class User {
|
|||||||
builder.append("User [name=").append(name).append(", id=").append(id).append("]");
|
builder.append("User [name=").append(name).append(", id=").append(id).append("]");
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,20 +0,0 @@
|
|||||||
package com.baeldung.persistence.dao.common;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public interface IOperations<T extends Serializable> {
|
|
||||||
|
|
||||||
T findOne(final long id);
|
|
||||||
|
|
||||||
List<T> findAll();
|
|
||||||
|
|
||||||
void create(final T entity);
|
|
||||||
|
|
||||||
T update(final T entity);
|
|
||||||
|
|
||||||
void delete(final T entity);
|
|
||||||
|
|
||||||
void deleteById(final long entityId);
|
|
||||||
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
package com.baeldung.persistence.service;
|
|
||||||
|
|
||||||
import com.baeldung.persistence.dao.common.IOperations;
|
|
||||||
import com.baeldung.persistence.model.Bar;
|
|
||||||
|
|
||||||
public interface IBarService extends IOperations<Bar> {
|
|
||||||
//
|
|
||||||
}
|
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.baeldung.services;
|
||||||
|
|
||||||
|
import com.baeldung.domain.Bar;
|
||||||
|
|
||||||
|
public interface IBarService extends IOperations<Bar> {
|
||||||
|
//
|
||||||
|
}
|
@ -1,7 +1,6 @@
|
|||||||
package org.baeldung.persistence.service;
|
package com.baeldung.services;
|
||||||
|
|
||||||
import org.baeldung.persistence.IOperations;
|
import com.baeldung.domain.Foo;
|
||||||
import org.baeldung.persistence.model.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;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.persistence;
|
package com.baeldung.services;
|
||||||
|
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
|
|
||||||
@ -7,12 +7,8 @@ import java.util.List;
|
|||||||
|
|
||||||
public interface IOperations<T extends Serializable> {
|
public interface IOperations<T extends Serializable> {
|
||||||
|
|
||||||
// read - one
|
|
||||||
|
|
||||||
T findOne(final long id);
|
T findOne(final long id);
|
||||||
|
|
||||||
// read - all
|
|
||||||
|
|
||||||
List<T> findAll();
|
List<T> findAll();
|
||||||
|
|
||||||
Page<T> findPaginated(int page, int size);
|
Page<T> findPaginated(int page, int size);
|
@ -1,7 +1,7 @@
|
|||||||
package org.baeldung.persistence.service.common;
|
package com.baeldung.services.impl;
|
||||||
|
|
||||||
|
import com.baeldung.services.IOperations;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import org.baeldung.persistence.IOperations;
|
|
||||||
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.repository.PagingAndSortingRepository;
|
import org.springframework.data.repository.PagingAndSortingRepository;
|
@ -1,6 +1,6 @@
|
|||||||
package com.baeldung.persistence.service.common;
|
package com.baeldung.services.impl;
|
||||||
|
|
||||||
import com.baeldung.persistence.dao.common.IOperations;
|
import com.baeldung.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;
|
||||||
@ -8,12 +8,12 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Transactional(value = "jpaTransactionManager")
|
@Transactional(value = "transactionManager")
|
||||||
public abstract class AbstractSpringDataJpaService<T extends Serializable> implements IOperations<T> {
|
public abstract class AbstractSpringDataJpaService<T extends Serializable> implements IOperations<T> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T findOne(final long id) {
|
public T findOne(final long id) {
|
||||||
return getDao().findOne(Long.valueOf(id));
|
return getDao().findById(id).orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -22,8 +22,8 @@ public abstract class AbstractSpringDataJpaService<T extends Serializable> imple
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void create(final T entity) {
|
public T create(final T entity) {
|
||||||
getDao().save(entity);
|
return getDao().save(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -38,7 +38,7 @@ public abstract class AbstractSpringDataJpaService<T extends Serializable> imple
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deleteById(final long entityId) {
|
public void deleteById(final long entityId) {
|
||||||
getDao().delete(Long.valueOf(entityId));
|
getDao().deleteById(entityId);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract CrudRepository<T, Serializable> getDao();
|
protected abstract CrudRepository<T, Serializable> getDao();
|
@ -1,10 +1,10 @@
|
|||||||
package com.baeldung.persistence.service.impl;
|
package com.baeldung.services.impl;
|
||||||
|
|
||||||
import com.baeldung.persistence.dao.IBarCrudRepository;
|
import com.baeldung.domain.Bar;
|
||||||
import com.baeldung.persistence.model.Bar;
|
import com.baeldung.dao.repositories.IBarCrudRepository;
|
||||||
import com.baeldung.persistence.service.IBarService;
|
import com.baeldung.services.IBarService;
|
||||||
import com.baeldung.persistence.service.common.AbstractSpringDataJpaService;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
@ -23,4 +23,8 @@ public class BarSpringDataJpaService extends AbstractSpringDataJpaService<Bar> i
|
|||||||
return dao;
|
return dao;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Page<Bar> findPaginated(int page, int size) {
|
||||||
|
throw new UnsupportedOperationException("Not implemented yet");
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,10 +1,9 @@
|
|||||||
package org.baeldung.persistence.service.impl;
|
package com.baeldung.services.impl;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import org.baeldung.persistence.dao.IFooDao;
|
import com.baeldung.dao.IFooDao;
|
||||||
import org.baeldung.persistence.model.Foo;
|
import com.baeldung.domain.Foo;
|
||||||
import org.baeldung.persistence.service.IFooService;
|
import com.baeldung.services.IFooService;
|
||||||
import org.baeldung.persistence.service.common.AbstractService;
|
|
||||||
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,169 +0,0 @@
|
|||||||
package com.baeldung.spring;
|
|
||||||
|
|
||||||
import com.baeldung.persistence.dao.IBarAuditableDao;
|
|
||||||
import com.baeldung.persistence.dao.IBarDao;
|
|
||||||
import com.baeldung.persistence.dao.IFooAuditableDao;
|
|
||||||
import com.baeldung.persistence.dao.IFooDao;
|
|
||||||
import com.baeldung.persistence.dao.impl.*;
|
|
||||||
import com.baeldung.persistence.service.IBarAuditableService;
|
|
||||||
import com.baeldung.persistence.service.IBarService;
|
|
||||||
import com.baeldung.persistence.service.IFooAuditableService;
|
|
||||||
import com.baeldung.persistence.service.IFooService;
|
|
||||||
import com.baeldung.persistence.service.impl.*;
|
|
||||||
import com.google.common.base.Preconditions;
|
|
||||||
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.context.annotation.PropertySource;
|
|
||||||
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.orm.hibernate4.HibernateTransactionManager;
|
|
||||||
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
|
|
||||||
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
|
|
||||||
@EnableTransactionManagement
|
|
||||||
@EnableJpaRepositories(basePackages = { "com.baeldung.persistence" }, transactionManagerRef = "jpaTransactionManager")
|
|
||||||
@EnableJpaAuditing
|
|
||||||
@PropertySource({ "classpath:persistence-mysql.properties" })
|
|
||||||
@ComponentScan({ "com.baeldung.persistence" })
|
|
||||||
public class PersistenceConfig {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private Environment env;
|
|
||||||
|
|
||||||
public PersistenceConfig() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public LocalSessionFactoryBean sessionFactory() {
|
|
||||||
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
|
|
||||||
sessionFactory.setDataSource(restDataSource());
|
|
||||||
sessionFactory.setPackagesToScan(new String[] { "com.baeldung.persistence.model" });
|
|
||||||
sessionFactory.setHibernateProperties(hibernateProperties());
|
|
||||||
|
|
||||||
return sessionFactory;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
|
||||||
final LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
|
|
||||||
emf.setDataSource(restDataSource());
|
|
||||||
emf.setPackagesToScan(new String[] { "com.baeldung.persistence.model" });
|
|
||||||
|
|
||||||
final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
|
||||||
emf.setJpaVendorAdapter(vendorAdapter);
|
|
||||||
emf.setJpaProperties(hibernateProperties());
|
|
||||||
|
|
||||||
return emf;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public DataSource restDataSource() {
|
|
||||||
final BasicDataSource dataSource = new BasicDataSource();
|
|
||||||
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 hibernateTransactionManager() {
|
|
||||||
final HibernateTransactionManager transactionManager = new HibernateTransactionManager();
|
|
||||||
transactionManager.setSessionFactory(sessionFactory().getObject());
|
|
||||||
return transactionManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public PlatformTransactionManager jpaTransactionManager() {
|
|
||||||
final JpaTransactionManager transactionManager = new JpaTransactionManager();
|
|
||||||
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
|
|
||||||
return transactionManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
|
|
||||||
return new PersistenceExceptionTranslationPostProcessor();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public IBarService barJpaService() {
|
|
||||||
return new BarJpaService();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public IBarService barSpringDataJpaService() {
|
|
||||||
return new BarSpringDataJpaService();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public IFooService fooHibernateService() {
|
|
||||||
return new FooService();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public IBarAuditableService barHibernateAuditableService() {
|
|
||||||
return new BarAuditableService();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public IFooAuditableService fooHibernateAuditableService() {
|
|
||||||
return new FooAuditableService();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public IBarDao barJpaDao() {
|
|
||||||
return new BarJpaDao();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public IBarDao barHibernateDao() {
|
|
||||||
return new BarDao();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public IBarAuditableDao barHibernateAuditableDao() {
|
|
||||||
return new BarAuditableDao();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public IFooDao fooHibernateDao() {
|
|
||||||
return new FooDao();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public IFooAuditableDao fooHibernateAuditableDao() {
|
|
||||||
return new FooAuditableDao();
|
|
||||||
}
|
|
||||||
|
|
||||||
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,66 +0,0 @@
|
|||||||
package org.baeldung.config;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.context.annotation.PropertySource;
|
|
||||||
import org.springframework.core.env.Environment;
|
|
||||||
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.LocalContainerEntityManagerFactoryBean;
|
|
||||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
|
||||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|
||||||
|
|
||||||
import javax.persistence.EntityManagerFactory;
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@EnableJpaRepositories(basePackages = {"org.baeldung.repository" })
|
|
||||||
@PropertySource("classpath:persistence-generic-entity.properties")
|
|
||||||
@EnableTransactionManagement
|
|
||||||
public class H2JpaConfig {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private Environment env;
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public DataSource dataSource() {
|
|
||||||
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
|
||||||
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
|
|
||||||
dataSource.setUrl(env.getProperty("jdbc.url"));
|
|
||||||
dataSource.setUsername(env.getProperty("jdbc.user"));
|
|
||||||
dataSource.setPassword(env.getProperty("jdbc.pass"));
|
|
||||||
|
|
||||||
return dataSource;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
|
||||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
|
||||||
em.setDataSource(dataSource());
|
|
||||||
em.setPackagesToScan("org.baeldung.persistence.model");
|
|
||||||
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
|
|
||||||
em.setJpaProperties(additionalProperties());
|
|
||||||
return em;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
JpaTransactionManager transactionManager(final EntityManagerFactory entityManagerFactory) {
|
|
||||||
final JpaTransactionManager transactionManager = new JpaTransactionManager();
|
|
||||||
transactionManager.setEntityManagerFactory(entityManagerFactory);
|
|
||||||
return transactionManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
final Properties additionalProperties() {
|
|
||||||
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", env.getProperty("hibernate.show_sql"));
|
|
||||||
|
|
||||||
return hibernateProperties;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,89 +0,0 @@
|
|||||||
package org.baeldung.config;
|
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.context.annotation.PropertySource;
|
|
||||||
import org.springframework.core.env.Environment;
|
|
||||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
|
||||||
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.LocalContainerEntityManagerFactoryBean;
|
|
||||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
|
||||||
import org.springframework.transaction.PlatformTransactionManager;
|
|
||||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|
||||||
|
|
||||||
import javax.persistence.EntityManagerFactory;
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@EnableTransactionManagement
|
|
||||||
@PropertySource({ "classpath:persistence-h2.properties" })
|
|
||||||
@ComponentScan({ "org.baeldung.persistence" })
|
|
||||||
@EnableJpaRepositories(basePackages = { "org.baeldung.persistence.dao", "org.baeldung.persistence.repository" })
|
|
||||||
public class PersistenceJPAConfigL2Cache {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private Environment env;
|
|
||||||
|
|
||||||
public PersistenceJPAConfigL2Cache() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
// beans
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
|
||||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
|
||||||
em.setDataSource(dataSource());
|
|
||||||
em.setPackagesToScan(getPackagesToScan());
|
|
||||||
|
|
||||||
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
|
||||||
em.setJpaVendorAdapter(vendorAdapter);
|
|
||||||
em.setJpaProperties(additionalProperties());
|
|
||||||
|
|
||||||
return em;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected String[] getPackagesToScan() {
|
|
||||||
return new String[] { "org.baeldung.persistence.model" };
|
|
||||||
}
|
|
||||||
|
|
||||||
@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")));
|
|
||||||
|
|
||||||
return dataSource;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) {
|
|
||||||
final JpaTransactionManager transactionManager = new JpaTransactionManager();
|
|
||||||
transactionManager.setEntityManagerFactory(emf);
|
|
||||||
return transactionManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
|
|
||||||
return new PersistenceExceptionTranslationPostProcessor();
|
|
||||||
}
|
|
||||||
|
|
||||||
final Properties additionalProperties() {
|
|
||||||
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.cache.use_second_level_cache", env.getProperty("hibernate.cache.use_second_level_cache"));
|
|
||||||
hibernateProperties.setProperty("hibernate.cache.use_query_cache", env.getProperty("hibernate.cache.use_query_cache"));
|
|
||||||
hibernateProperties.setProperty("hibernate.cache.region.factory_class", env.getProperty("hibernate.cache.region.factory_class"));
|
|
||||||
hibernateProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
|
|
||||||
return hibernateProperties;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,69 +0,0 @@
|
|||||||
package org.baeldung.config;
|
|
||||||
|
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
import javax.persistence.EntityManagerFactory;
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
|
|
||||||
import org.baeldung.extended.persistence.dao.ExtendedRepositoryImpl;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.context.annotation.PropertySource;
|
|
||||||
import org.springframework.core.env.Environment;
|
|
||||||
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.LocalContainerEntityManagerFactoryBean;
|
|
||||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
|
||||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@EnableJpaRepositories(basePackages = "org.baeldung.extended.persistence.dao", repositoryBaseClass = ExtendedRepositoryImpl.class)
|
|
||||||
@PropertySource("persistence-student-h2.properties")
|
|
||||||
@EnableTransactionManagement
|
|
||||||
public class StudentJPAH2Config {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private Environment env;
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public DataSource dataSource() {
|
|
||||||
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
|
||||||
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
|
|
||||||
dataSource.setUrl(env.getProperty("jdbc.url"));
|
|
||||||
dataSource.setUsername(env.getProperty("jdbc.user"));
|
|
||||||
dataSource.setPassword(env.getProperty("jdbc.pass"));
|
|
||||||
|
|
||||||
return dataSource;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
|
||||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
|
||||||
em.setDataSource(dataSource());
|
|
||||||
em.setPackagesToScan(new String[] { "org.baeldung.inmemory.persistence.model" });
|
|
||||||
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
|
|
||||||
em.setJpaProperties(additionalProperties());
|
|
||||||
return em;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
|
|
||||||
JpaTransactionManager transactionManager = new JpaTransactionManager();
|
|
||||||
transactionManager.setEntityManagerFactory(entityManagerFactory);
|
|
||||||
return transactionManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
final Properties additionalProperties() {
|
|
||||||
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", env.getProperty("hibernate.show_sql"));
|
|
||||||
hibernateProperties.setProperty("hibernate.cache.use_second_level_cache", env.getProperty("hibernate.cache.use_second_level_cache"));
|
|
||||||
hibernateProperties.setProperty("hibernate.cache.use_query_cache", env.getProperty("hibernate.cache.use_query_cache"));
|
|
||||||
|
|
||||||
return hibernateProperties;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
package org.baeldung.extended.persistence.dao;
|
|
||||||
|
|
||||||
import org.baeldung.inmemory.persistence.model.Student;
|
|
||||||
|
|
||||||
public interface ExtendedStudentRepository extends ExtendedRepository<Student, Long> {
|
|
||||||
}
|
|
@ -1,78 +0,0 @@
|
|||||||
package org.baeldung.persistence.model;
|
|
||||||
|
|
||||||
import javax.persistence.*;
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
@Entity
|
|
||||||
public class Foo implements Serializable {
|
|
||||||
|
|
||||||
@Id
|
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
|
||||||
private long id;
|
|
||||||
|
|
||||||
@Column(nullable = false)
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
public Foo() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Foo(final String name) {
|
|
||||||
super();
|
|
||||||
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
// API
|
|
||||||
|
|
||||||
public long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(final long 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 + ((name == null) ? 0 : name.hashCode());
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(final Object obj) {
|
|
||||||
if (this == obj)
|
|
||||||
return true;
|
|
||||||
if (obj == null)
|
|
||||||
return false;
|
|
||||||
if (getClass() != obj.getClass())
|
|
||||||
return false;
|
|
||||||
final Foo other = (Foo) obj;
|
|
||||||
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("Foo [name=").append(name).append("]");
|
|
||||||
return builder.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,49 +0,0 @@
|
|||||||
package org.baeldung.persistence.model;
|
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
|
||||||
import javax.persistence.GeneratedValue;
|
|
||||||
import javax.persistence.Id;
|
|
||||||
import javax.persistence.Table;
|
|
||||||
|
|
||||||
@Entity
|
|
||||||
@Table(name = "users")
|
|
||||||
public class User {
|
|
||||||
|
|
||||||
@Id
|
|
||||||
@GeneratedValue
|
|
||||||
private Integer id;
|
|
||||||
private String name;
|
|
||||||
private Integer status;
|
|
||||||
|
|
||||||
public User() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public User(String name, Integer status) {
|
|
||||||
this.name = name;
|
|
||||||
this.status = status;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Integer getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(Integer id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Integer getStatus() {
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setStatus(Integer status) {
|
|
||||||
this.status = status;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
package org.baeldung.persistence.multiple.dao.user;
|
|
||||||
|
|
||||||
import org.baeldung.persistence.multiple.model.user.User;
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
|
|
||||||
public interface UserRepository extends JpaRepository<User, Integer> {
|
|
||||||
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
package org.baeldung.repository;
|
|
||||||
|
|
||||||
import org.baeldung.persistence.model.User;
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
|
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
@Repository("userRepository")
|
|
||||||
public interface UserRepository extends JpaRepository<User, Integer> {
|
|
||||||
|
|
||||||
Stream<User> findAllByName(String name);
|
|
||||||
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
jdbc.driverClassName=org.h2.Driver
|
|
||||||
jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
|
|
||||||
jdbc.user=sa
|
|
||||||
jdbc.pass=sa
|
|
||||||
|
|
||||||
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
|
||||||
hibernate.show_sql=true
|
|
||||||
hibernate.hbm2ddl.auto=create-drop
|
|
@ -1,7 +1,7 @@
|
|||||||
# jdbc.X
|
# jdbc.X
|
||||||
jdbc.driverClassName=org.h2.Driver
|
jdbc.driverClassName=org.h2.Driver
|
||||||
user.jdbc.url=jdbc:h2:mem:spring_jpa_user;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS SPRING_JPA_USER
|
user.jdbc.url=jdbc:h2:mem:spring_jpa_user;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS USERS
|
||||||
product.jdbc.url=jdbc:h2:mem:spring_jpa_product;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS SPRING_JPA_PRODUCT
|
product.jdbc.url=jdbc:h2:mem:spring_jpa_product;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS PRODUCTS
|
||||||
jdbc.user=sa
|
jdbc.user=sa
|
||||||
jdbc.pass=
|
jdbc.pass=
|
||||||
|
|
||||||
|
@ -1,12 +0,0 @@
|
|||||||
# jdbc.X
|
|
||||||
jdbc.driverClassName=org.h2.Driver
|
|
||||||
jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
|
|
||||||
jdbc.user=sa
|
|
||||||
jdbc.pass=
|
|
||||||
|
|
||||||
# hibernate.X
|
|
||||||
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
|
||||||
hibernate.show_sql=true
|
|
||||||
hibernate.hbm2ddl.auto=create-drop
|
|
||||||
hibernate.cache.use_second_level_cache=false
|
|
||||||
hibernate.cache.use_query_cache=false
|
|
@ -1,6 +1,6 @@
|
|||||||
# jdbc.X
|
# jdbc.X
|
||||||
jdbc.driverClassName=org.h2.Driver
|
jdbc.driverClassName=org.h2.Driver
|
||||||
jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
|
jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS USERS
|
||||||
jdbc.user=sa
|
jdbc.user=sa
|
||||||
jdbc.pass=
|
jdbc.pass=
|
||||||
|
|
||||||
@ -11,3 +11,6 @@ hibernate.hbm2ddl.auto=create-drop
|
|||||||
hibernate.cache.use_second_level_cache=true
|
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
|
@ -1,7 +1,9 @@
|
|||||||
package com.baeldung.repository;
|
package com.baeldung.dao.repositories;
|
||||||
|
|
||||||
|
import com.baeldung.config.PersistenceConfiguration;
|
||||||
|
import com.baeldung.config.PersistenceProductConfiguration;
|
||||||
|
import com.baeldung.config.PersistenceUserConfiguration;
|
||||||
import com.baeldung.domain.Article;
|
import com.baeldung.domain.Article;
|
||||||
import com.baeldung.repository.ArticleRepository;
|
|
||||||
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;
|
||||||
@ -16,7 +18,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
|
@DataJpaTest(excludeAutoConfiguration = {PersistenceConfiguration.class, PersistenceUserConfiguration.class, PersistenceProductConfiguration.class})
|
||||||
public class ArticleRepositoryIntegrationTest {
|
public class ArticleRepositoryIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
@ -1,23 +1,21 @@
|
|||||||
package org.baeldung.persistence.repository;
|
package com.baeldung.dao.repositories;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import com.baeldung.config.PersistenceConfiguration;
|
||||||
|
import com.baeldung.domain.Student;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
|
|
||||||
import org.baeldung.config.StudentJPAH2Config;
|
|
||||||
import org.baeldung.extended.persistence.dao.ExtendedStudentRepository;
|
|
||||||
import org.baeldung.inmemory.persistence.model.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;
|
||||||
import org.springframework.test.annotation.DirtiesContext;
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
import javax.annotation.Resource;
|
||||||
@ContextConfiguration(classes = { StudentJPAH2Config.class })
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@ContextConfiguration(classes = {PersistenceConfiguration.class})
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
public class ExtendedStudentRepositoryIntegrationTest {
|
public class ExtendedStudentRepositoryIntegrationTest {
|
||||||
@Resource
|
@Resource
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.repository;
|
package com.baeldung.dao.repositories;
|
||||||
|
|
||||||
import static junit.framework.TestCase.assertEquals;
|
import static junit.framework.TestCase.assertEquals;
|
||||||
import static junit.framework.TestCase.assertFalse;
|
import static junit.framework.TestCase.assertFalse;
|
||||||
@ -9,10 +9,19 @@ import static junit.framework.TestCase.assertTrue;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import com.baeldung.config.PersistenceConfiguration;
|
||||||
|
import com.baeldung.config.PersistenceProductConfiguration;
|
||||||
|
import com.baeldung.config.PersistenceUserConfiguration;
|
||||||
|
import com.baeldung.dao.repositories.ItemTypeRepository;
|
||||||
|
import com.baeldung.dao.repositories.LocationRepository;
|
||||||
|
import com.baeldung.dao.repositories.ReadOnlyLocationRepository;
|
||||||
|
import com.baeldung.dao.repositories.StoreRepository;
|
||||||
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.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import com.baeldung.domain.Item;
|
import com.baeldung.domain.Item;
|
||||||
@ -21,7 +30,7 @@ import com.baeldung.domain.Location;
|
|||||||
import com.baeldung.domain.Store;
|
import com.baeldung.domain.Store;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@DataJpaTest
|
@DataJpaTest(excludeAutoConfiguration = {PersistenceConfiguration.class, PersistenceUserConfiguration.class, PersistenceProductConfiguration.class})
|
||||||
public class JpaRepositoriesIntegrationTest {
|
public class JpaRepositoriesIntegrationTest {
|
||||||
@Autowired
|
@Autowired
|
||||||
private LocationRepository locationRepository;
|
private LocationRepository locationRepository;
|
@ -1,23 +1,25 @@
|
|||||||
package org.baeldung.persistence.repository;
|
package com.baeldung.dao.repositories;
|
||||||
|
|
||||||
import org.baeldung.config.PersistenceJPAConfigL2Cache;
|
import com.baeldung.config.PersistenceConfiguration;
|
||||||
import org.baeldung.persistence.model.User;
|
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.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.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.data.jpa.domain.JpaSort;
|
import org.springframework.data.jpa.domain.JpaSort;
|
||||||
import org.springframework.data.mapping.PropertyReferenceException;
|
import org.springframework.data.mapping.PropertyReferenceException;
|
||||||
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.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@ -25,32 +27,71 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||||||
* Created by adam.
|
* Created by adam.
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@ContextConfiguration(classes = PersistenceJPAConfigL2Cache.class)
|
@SpringBootTest(classes = PersistenceConfiguration.class)
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
public class UserRepositoryIntegrationTest {
|
public class UserRepositoryIntegrationTest {
|
||||||
|
|
||||||
private final String USER_NAME_ADAM = "Adam";
|
private final String USER_NAME_ADAM = "Adam";
|
||||||
private final String USER_NAME_PETER = "Peter";
|
private final String USER_NAME_PETER = "Peter";
|
||||||
|
|
||||||
|
private final String USER_EMAIL = "email@example.com";
|
||||||
|
private final String USER_EMAIL2 = "email2@example.com";
|
||||||
|
private final String USER_EMAIL3 = "email3@example.com";
|
||||||
|
private final String USER_EMAIL4 = "email4@example.com";
|
||||||
|
private final String USER_EMAIL5 = "email5@example.com";
|
||||||
|
private final String USER_EMAIL6 = "email6@example.com";
|
||||||
|
|
||||||
private final Integer INACTIVE_STATUS = 0;
|
private final Integer INACTIVE_STATUS = 0;
|
||||||
private final Integer ACTIVE_STATUS = 1;
|
private final Integer ACTIVE_STATUS = 1;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserRepository userRepository;
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Transactional
|
||||||
|
public void givenUsersWithSameNameInDBWhenFindAllByNameThenReturnStreamOfUsers() {
|
||||||
|
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
|
@Test
|
||||||
public void givenUsersInDBWhenFindAllWithQueryAnnotationThenReturnCollectionWithActiveUsers() {
|
public void givenUsersInDBWhenFindAllWithQueryAnnotationThenReturnCollectionWithActiveUsers() {
|
||||||
User user1 = new User();
|
User user1 = new User();
|
||||||
user1.setName(USER_NAME_ADAM);
|
user1.setName(USER_NAME_ADAM);
|
||||||
|
user1.setEmail(USER_EMAIL);
|
||||||
user1.setStatus(ACTIVE_STATUS);
|
user1.setStatus(ACTIVE_STATUS);
|
||||||
userRepository.save(user1);
|
userRepository.save(user1);
|
||||||
|
|
||||||
User user2 = new User();
|
User user2 = new User();
|
||||||
user2.setName(USER_NAME_ADAM);
|
user2.setName(USER_NAME_ADAM);
|
||||||
|
user2.setEmail(USER_EMAIL2);
|
||||||
user2.setStatus(ACTIVE_STATUS);
|
user2.setStatus(ACTIVE_STATUS);
|
||||||
userRepository.save(user2);
|
userRepository.save(user2);
|
||||||
|
|
||||||
User user3 = new User();
|
User user3 = new User();
|
||||||
user3.setName(USER_NAME_ADAM);
|
user3.setName(USER_NAME_ADAM);
|
||||||
|
user3.setEmail(USER_EMAIL3);
|
||||||
user3.setStatus(INACTIVE_STATUS);
|
user3.setStatus(INACTIVE_STATUS);
|
||||||
userRepository.save(user3);
|
userRepository.save(user3);
|
||||||
|
|
||||||
@ -63,16 +104,19 @@ public class UserRepositoryIntegrationTest {
|
|||||||
public void givenUsersInDBWhenFindAllWithQueryAnnotationNativeThenReturnCollectionWithActiveUsers() {
|
public void givenUsersInDBWhenFindAllWithQueryAnnotationNativeThenReturnCollectionWithActiveUsers() {
|
||||||
User user1 = new User();
|
User user1 = new User();
|
||||||
user1.setName(USER_NAME_ADAM);
|
user1.setName(USER_NAME_ADAM);
|
||||||
|
user1.setEmail(USER_EMAIL);
|
||||||
user1.setStatus(ACTIVE_STATUS);
|
user1.setStatus(ACTIVE_STATUS);
|
||||||
userRepository.save(user1);
|
userRepository.save(user1);
|
||||||
|
|
||||||
User user2 = new User();
|
User user2 = new User();
|
||||||
user2.setName(USER_NAME_ADAM);
|
user2.setName(USER_NAME_ADAM);
|
||||||
|
user2.setEmail(USER_EMAIL2);
|
||||||
user2.setStatus(ACTIVE_STATUS);
|
user2.setStatus(ACTIVE_STATUS);
|
||||||
userRepository.save(user2);
|
userRepository.save(user2);
|
||||||
|
|
||||||
User user3 = new User();
|
User user3 = new User();
|
||||||
user3.setName(USER_NAME_ADAM);
|
user3.setName(USER_NAME_ADAM);
|
||||||
|
user3.setEmail(USER_EMAIL3);
|
||||||
user3.setStatus(INACTIVE_STATUS);
|
user3.setStatus(INACTIVE_STATUS);
|
||||||
userRepository.save(user3);
|
userRepository.save(user3);
|
||||||
|
|
||||||
@ -85,6 +129,7 @@ public class UserRepositoryIntegrationTest {
|
|||||||
public void givenUserInDBWhenFindUserByStatusWithQueryAnnotationThenReturnActiveUser() {
|
public void givenUserInDBWhenFindUserByStatusWithQueryAnnotationThenReturnActiveUser() {
|
||||||
User user = new User();
|
User user = new User();
|
||||||
user.setName(USER_NAME_ADAM);
|
user.setName(USER_NAME_ADAM);
|
||||||
|
user.setEmail(USER_EMAIL);
|
||||||
user.setStatus(ACTIVE_STATUS);
|
user.setStatus(ACTIVE_STATUS);
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
|
|
||||||
@ -97,6 +142,7 @@ public class UserRepositoryIntegrationTest {
|
|||||||
public void givenUserInDBWhenFindUserByStatusWithQueryAnnotationNativeThenReturnActiveUser() {
|
public void givenUserInDBWhenFindUserByStatusWithQueryAnnotationNativeThenReturnActiveUser() {
|
||||||
User user = new User();
|
User user = new User();
|
||||||
user.setName(USER_NAME_ADAM);
|
user.setName(USER_NAME_ADAM);
|
||||||
|
user.setEmail(USER_EMAIL);
|
||||||
user.setStatus(ACTIVE_STATUS);
|
user.setStatus(ACTIVE_STATUS);
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
|
|
||||||
@ -109,11 +155,13 @@ public class UserRepositoryIntegrationTest {
|
|||||||
public void givenUsersInDBWhenFindUserByStatusAndNameWithQueryAnnotationIndexedParamsThenReturnOneUser() {
|
public void givenUsersInDBWhenFindUserByStatusAndNameWithQueryAnnotationIndexedParamsThenReturnOneUser() {
|
||||||
User user = new User();
|
User user = new User();
|
||||||
user.setName(USER_NAME_ADAM);
|
user.setName(USER_NAME_ADAM);
|
||||||
|
user.setEmail(USER_EMAIL);
|
||||||
user.setStatus(ACTIVE_STATUS);
|
user.setStatus(ACTIVE_STATUS);
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
|
|
||||||
User user2 = new User();
|
User user2 = new User();
|
||||||
user2.setName(USER_NAME_PETER);
|
user2.setName(USER_NAME_PETER);
|
||||||
|
user2.setEmail(USER_EMAIL2);
|
||||||
user2.setStatus(ACTIVE_STATUS);
|
user2.setStatus(ACTIVE_STATUS);
|
||||||
userRepository.save(user2);
|
userRepository.save(user2);
|
||||||
|
|
||||||
@ -126,11 +174,13 @@ public class UserRepositoryIntegrationTest {
|
|||||||
public void givenUsersInDBWhenFindUserByStatusAndNameWithQueryAnnotationNamedParamsThenReturnOneUser() {
|
public void givenUsersInDBWhenFindUserByStatusAndNameWithQueryAnnotationNamedParamsThenReturnOneUser() {
|
||||||
User user = new User();
|
User user = new User();
|
||||||
user.setName(USER_NAME_ADAM);
|
user.setName(USER_NAME_ADAM);
|
||||||
|
user.setEmail(USER_EMAIL);
|
||||||
user.setStatus(ACTIVE_STATUS);
|
user.setStatus(ACTIVE_STATUS);
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
|
|
||||||
User user2 = new User();
|
User user2 = new User();
|
||||||
user2.setName(USER_NAME_PETER);
|
user2.setName(USER_NAME_PETER);
|
||||||
|
user2.setEmail(USER_EMAIL2);
|
||||||
user2.setStatus(ACTIVE_STATUS);
|
user2.setStatus(ACTIVE_STATUS);
|
||||||
userRepository.save(user2);
|
userRepository.save(user2);
|
||||||
|
|
||||||
@ -143,11 +193,13 @@ public class UserRepositoryIntegrationTest {
|
|||||||
public void givenUsersInDBWhenFindUserByStatusAndNameWithQueryAnnotationNativeNamedParamsThenReturnOneUser() {
|
public void givenUsersInDBWhenFindUserByStatusAndNameWithQueryAnnotationNativeNamedParamsThenReturnOneUser() {
|
||||||
User user = new User();
|
User user = new User();
|
||||||
user.setName(USER_NAME_ADAM);
|
user.setName(USER_NAME_ADAM);
|
||||||
|
user.setEmail(USER_EMAIL);
|
||||||
user.setStatus(ACTIVE_STATUS);
|
user.setStatus(ACTIVE_STATUS);
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
|
|
||||||
User user2 = new User();
|
User user2 = new User();
|
||||||
user2.setName(USER_NAME_PETER);
|
user2.setName(USER_NAME_PETER);
|
||||||
|
user2.setEmail(USER_EMAIL2);
|
||||||
user2.setStatus(ACTIVE_STATUS);
|
user2.setStatus(ACTIVE_STATUS);
|
||||||
userRepository.save(user2);
|
userRepository.save(user2);
|
||||||
|
|
||||||
@ -160,11 +212,13 @@ public class UserRepositoryIntegrationTest {
|
|||||||
public void givenUsersInDBWhenFindUserByStatusAndNameWithQueryAnnotationNamedParamsCustomNamesThenReturnOneUser() {
|
public void givenUsersInDBWhenFindUserByStatusAndNameWithQueryAnnotationNamedParamsCustomNamesThenReturnOneUser() {
|
||||||
User user = new User();
|
User user = new User();
|
||||||
user.setName(USER_NAME_ADAM);
|
user.setName(USER_NAME_ADAM);
|
||||||
|
user.setEmail(USER_EMAIL);
|
||||||
user.setStatus(ACTIVE_STATUS);
|
user.setStatus(ACTIVE_STATUS);
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
|
|
||||||
User user2 = new User();
|
User user2 = new User();
|
||||||
user2.setName(USER_NAME_PETER);
|
user2.setName(USER_NAME_PETER);
|
||||||
|
user2.setEmail(USER_EMAIL2);
|
||||||
user2.setStatus(ACTIVE_STATUS);
|
user2.setStatus(ACTIVE_STATUS);
|
||||||
userRepository.save(user2);
|
userRepository.save(user2);
|
||||||
|
|
||||||
@ -177,6 +231,7 @@ public class UserRepositoryIntegrationTest {
|
|||||||
public void givenUsersInDBWhenFindUserByNameLikeWithQueryAnnotationIndexedParamsThenReturnUser() {
|
public void givenUsersInDBWhenFindUserByNameLikeWithQueryAnnotationIndexedParamsThenReturnUser() {
|
||||||
User user = new User();
|
User user = new User();
|
||||||
user.setName(USER_NAME_ADAM);
|
user.setName(USER_NAME_ADAM);
|
||||||
|
user.setEmail(USER_EMAIL);
|
||||||
user.setStatus(ACTIVE_STATUS);
|
user.setStatus(ACTIVE_STATUS);
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
|
|
||||||
@ -189,6 +244,7 @@ public class UserRepositoryIntegrationTest {
|
|||||||
public void givenUsersInDBWhenFindUserByNameLikeWithQueryAnnotationNamedParamsThenReturnUser() {
|
public void givenUsersInDBWhenFindUserByNameLikeWithQueryAnnotationNamedParamsThenReturnUser() {
|
||||||
User user = new User();
|
User user = new User();
|
||||||
user.setName(USER_NAME_ADAM);
|
user.setName(USER_NAME_ADAM);
|
||||||
|
user.setEmail(USER_EMAIL);
|
||||||
user.setStatus(ACTIVE_STATUS);
|
user.setStatus(ACTIVE_STATUS);
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
|
|
||||||
@ -201,6 +257,7 @@ public class UserRepositoryIntegrationTest {
|
|||||||
public void givenUsersInDBWhenFindUserByNameLikeWithQueryAnnotationNativeThenReturnUser() {
|
public void givenUsersInDBWhenFindUserByNameLikeWithQueryAnnotationNativeThenReturnUser() {
|
||||||
User user = new User();
|
User user = new User();
|
||||||
user.setName(USER_NAME_ADAM);
|
user.setName(USER_NAME_ADAM);
|
||||||
|
user.setEmail(USER_EMAIL);
|
||||||
user.setStatus(ACTIVE_STATUS);
|
user.setStatus(ACTIVE_STATUS);
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
|
|
||||||
@ -211,9 +268,9 @@ public class UserRepositoryIntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUsersInDBWhenFindAllWithSortByNameThenReturnUsersSorted() {
|
public void givenUsersInDBWhenFindAllWithSortByNameThenReturnUsersSorted() {
|
||||||
userRepository.save(new User(USER_NAME_ADAM, ACTIVE_STATUS));
|
userRepository.save(new User(USER_NAME_ADAM, USER_EMAIL, ACTIVE_STATUS));
|
||||||
userRepository.save(new User(USER_NAME_PETER, ACTIVE_STATUS));
|
userRepository.save(new User(USER_NAME_PETER, USER_EMAIL2, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE", INACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE", USER_EMAIL3, INACTIVE_STATUS));
|
||||||
|
|
||||||
List<User> usersSortByName = userRepository.findAll(new Sort(Sort.Direction.ASC, "name"));
|
List<User> usersSortByName = userRepository.findAll(new Sort(Sort.Direction.ASC, "name"));
|
||||||
|
|
||||||
@ -224,9 +281,9 @@ public class UserRepositoryIntegrationTest {
|
|||||||
|
|
||||||
@Test(expected = PropertyReferenceException.class)
|
@Test(expected = PropertyReferenceException.class)
|
||||||
public void givenUsersInDBWhenFindAllSortWithFunctionThenThrowException() {
|
public void givenUsersInDBWhenFindAllSortWithFunctionThenThrowException() {
|
||||||
userRepository.save(new User(USER_NAME_ADAM, ACTIVE_STATUS));
|
userRepository.save(new User(USER_NAME_ADAM, USER_EMAIL, ACTIVE_STATUS));
|
||||||
userRepository.save(new User(USER_NAME_PETER, ACTIVE_STATUS));
|
userRepository.save(new User(USER_NAME_PETER, USER_EMAIL2, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE", INACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE", USER_EMAIL3, INACTIVE_STATUS));
|
||||||
|
|
||||||
userRepository.findAll(new Sort(Sort.Direction.ASC, "name"));
|
userRepository.findAll(new Sort(Sort.Direction.ASC, "name"));
|
||||||
|
|
||||||
@ -239,9 +296,9 @@ public class UserRepositoryIntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUsersInDBWhenFindAllSortWithFunctionQueryAnnotationJPQLThenReturnUsersSorted() {
|
public void givenUsersInDBWhenFindAllSortWithFunctionQueryAnnotationJPQLThenReturnUsersSorted() {
|
||||||
userRepository.save(new User(USER_NAME_ADAM, ACTIVE_STATUS));
|
userRepository.save(new User(USER_NAME_ADAM, USER_EMAIL, ACTIVE_STATUS));
|
||||||
userRepository.save(new User(USER_NAME_PETER, ACTIVE_STATUS));
|
userRepository.save(new User(USER_NAME_PETER, USER_EMAIL2, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE", INACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE", USER_EMAIL3, INACTIVE_STATUS));
|
||||||
|
|
||||||
userRepository.findAllUsers(new Sort("name"));
|
userRepository.findAllUsers(new Sort("name"));
|
||||||
|
|
||||||
@ -254,12 +311,12 @@ public class UserRepositoryIntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUsersInDBWhenFindAllWithPageRequestQueryAnnotationJPQLThenReturnPageOfUsers() {
|
public void givenUsersInDBWhenFindAllWithPageRequestQueryAnnotationJPQLThenReturnPageOfUsers() {
|
||||||
userRepository.save(new User(USER_NAME_ADAM, ACTIVE_STATUS));
|
userRepository.save(new User(USER_NAME_ADAM, USER_EMAIL, ACTIVE_STATUS));
|
||||||
userRepository.save(new User(USER_NAME_PETER, ACTIVE_STATUS));
|
userRepository.save(new User(USER_NAME_PETER, USER_EMAIL2, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE", INACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE", USER_EMAIL3, INACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE1", INACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE1", USER_EMAIL4, INACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE2", INACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE2", USER_EMAIL5, INACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE3", INACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE3", USER_EMAIL6, INACTIVE_STATUS));
|
||||||
|
|
||||||
Page<User> usersPage = userRepository.findAllUsersWithPagination(new PageRequest(1, 3));
|
Page<User> usersPage = userRepository.findAllUsersWithPagination(new PageRequest(1, 3));
|
||||||
|
|
||||||
@ -271,12 +328,12 @@ public class UserRepositoryIntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUsersInDBWhenFindAllWithPageRequestQueryAnnotationNativeThenReturnPageOfUsers() {
|
public void givenUsersInDBWhenFindAllWithPageRequestQueryAnnotationNativeThenReturnPageOfUsers() {
|
||||||
userRepository.save(new User(USER_NAME_ADAM, ACTIVE_STATUS));
|
userRepository.save(new User(USER_NAME_ADAM, USER_EMAIL, ACTIVE_STATUS));
|
||||||
userRepository.save(new User(USER_NAME_PETER, ACTIVE_STATUS));
|
userRepository.save(new User(USER_NAME_PETER, USER_EMAIL2, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE", INACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE", USER_EMAIL3, INACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE1", INACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE1", USER_EMAIL4, INACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE2", INACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE2", USER_EMAIL5, INACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE3", INACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE3", USER_EMAIL6, INACTIVE_STATUS));
|
||||||
|
|
||||||
Page<User> usersSortByNameLength = userRepository.findAllUsersWithPaginationNative(new PageRequest(1, 3));
|
Page<User> usersSortByNameLength = userRepository.findAllUsersWithPaginationNative(new PageRequest(1, 3));
|
||||||
|
|
||||||
@ -289,10 +346,10 @@ public class UserRepositoryIntegrationTest {
|
|||||||
@Test
|
@Test
|
||||||
@Transactional
|
@Transactional
|
||||||
public void givenUsersInDBWhenUpdateStatusForNameModifyingQueryAnnotationJPQLThenModifyMatchingUsers() {
|
public void givenUsersInDBWhenUpdateStatusForNameModifyingQueryAnnotationJPQLThenModifyMatchingUsers() {
|
||||||
userRepository.save(new User("SAMPLE", ACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE", USER_EMAIL, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE1", ACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE1", USER_EMAIL2, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE", ACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE", USER_EMAIL3, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE3", ACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE3", USER_EMAIL4, ACTIVE_STATUS));
|
||||||
|
|
||||||
int updatedUsersSize = userRepository.updateUserSetStatusForName(INACTIVE_STATUS, "SAMPLE");
|
int updatedUsersSize = userRepository.updateUserSetStatusForName(INACTIVE_STATUS, "SAMPLE");
|
||||||
|
|
||||||
@ -302,10 +359,10 @@ public class UserRepositoryIntegrationTest {
|
|||||||
@Test
|
@Test
|
||||||
@Transactional
|
@Transactional
|
||||||
public void givenUsersInDBWhenUpdateStatusForNameModifyingQueryAnnotationNativeThenModifyMatchingUsers() {
|
public void givenUsersInDBWhenUpdateStatusForNameModifyingQueryAnnotationNativeThenModifyMatchingUsers() {
|
||||||
userRepository.save(new User("SAMPLE", ACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE", USER_EMAIL, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE1", ACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE1", USER_EMAIL2, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE", ACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE", USER_EMAIL3, ACTIVE_STATUS));
|
||||||
userRepository.save(new User("SAMPLE3", ACTIVE_STATUS));
|
userRepository.save(new User("SAMPLE3", USER_EMAIL4, ACTIVE_STATUS));
|
||||||
userRepository.flush();
|
userRepository.flush();
|
||||||
|
|
||||||
int updatedUsersSize = userRepository.updateUserSetStatusForNameNative(INACTIVE_STATUS, "SAMPLE");
|
int updatedUsersSize = userRepository.updateUserSetStatusForNameNative(INACTIVE_STATUS, "SAMPLE");
|
@ -1,8 +1,7 @@
|
|||||||
package org.baeldung.persistence.service;
|
package com.baeldung.services;
|
||||||
|
|
||||||
import org.baeldung.persistence.IOperations;
|
import com.baeldung.domain.Foo;
|
||||||
import org.baeldung.persistence.model.Foo;
|
import com.baeldung.util.IDUtil;
|
||||||
import org.baeldung.util.IDUtil;
|
|
||||||
import org.hamcrest.Matchers;
|
import org.hamcrest.Matchers;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
@ -1,8 +1,7 @@
|
|||||||
package org.baeldung.persistence.service;
|
package com.baeldung.services;
|
||||||
|
|
||||||
import org.baeldung.persistence.IOperations;
|
import com.baeldung.config.PersistenceConfiguration;
|
||||||
import org.baeldung.persistence.model.Foo;
|
import com.baeldung.domain.Foo;
|
||||||
import org.baeldung.spring.PersistenceConfig;
|
|
||||||
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;
|
||||||
@ -10,14 +9,14 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
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;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||||
|
|
||||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
@ContextConfiguration(classes = {PersistenceConfiguration.class}, loader = AnnotationConfigContextLoader.class)
|
||||||
public class FooServicePersistenceIntegrationTest extends AbstractServicePersistenceIntegrationTest<Foo> {
|
public class FooServicePersistenceIntegrationTest extends AbstractServicePersistenceIntegrationTest<Foo> {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
@ -1,13 +1,13 @@
|
|||||||
package org.baeldung.persistence.service;
|
package com.baeldung.services;
|
||||||
|
|
||||||
import org.baeldung.config.ProductConfig;
|
import com.baeldung.config.PersistenceProductConfiguration;
|
||||||
import org.baeldung.config.UserConfig;
|
import com.baeldung.config.PersistenceUserConfiguration;
|
||||||
import org.baeldung.persistence.multiple.dao.product.ProductRepository;
|
import com.baeldung.dao.repositories.user.PossessionRepository;
|
||||||
import org.baeldung.persistence.multiple.dao.user.PossessionRepository;
|
import com.baeldung.dao.repositories.product.ProductRepository;
|
||||||
import org.baeldung.persistence.multiple.dao.user.UserRepository;
|
import com.baeldung.dao.repositories.user.UserRepository;
|
||||||
import org.baeldung.persistence.multiple.model.product.Product;
|
import com.baeldung.domain.user.Possession;
|
||||||
import org.baeldung.persistence.multiple.model.user.Possession;
|
import com.baeldung.domain.product.Product;
|
||||||
import org.baeldung.persistence.multiple.model.user.User;
|
import com.baeldung.domain.user.User;
|
||||||
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;
|
||||||
@ -15,6 +15,7 @@ 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.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
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;
|
||||||
|
|
||||||
@ -23,8 +24,8 @@ import java.util.Optional;
|
|||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@ContextConfiguration(classes = { UserConfig.class, ProductConfig.class })
|
@ContextConfiguration(classes = {PersistenceUserConfiguration.class, PersistenceProductConfiguration.class})
|
||||||
@EnableTransactionManagement
|
@EnableTransactionManagement
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
public class JpaMultipleDBIntegrationTest {
|
public class JpaMultipleDBIntegrationTest {
|
@ -1,8 +1,7 @@
|
|||||||
package com.baeldung.persistence.audit;
|
package com.baeldung.services;
|
||||||
|
|
||||||
import com.baeldung.persistence.model.Bar;
|
import com.baeldung.config.PersistenceConfiguration;
|
||||||
import com.baeldung.persistence.service.IBarService;
|
import com.baeldung.domain.Bar;
|
||||||
import com.baeldung.spring.config.PersistenceTestConfig;
|
|
||||||
import org.junit.*;
|
import org.junit.*;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@ -12,6 +11,7 @@ import org.springframework.beans.factory.annotation.Qualifier;
|
|||||||
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.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
@ -20,8 +20,8 @@ import javax.persistence.EntityManagerFactory;
|
|||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@ContextConfiguration(classes = { PersistenceTestConfig.class }, loader = AnnotationConfigContextLoader.class)
|
@ContextConfiguration(classes = { PersistenceConfiguration.class }, loader = AnnotationConfigContextLoader.class)
|
||||||
public class SpringDataJPABarAuditIntegrationTest {
|
public class SpringDataJPABarAuditIntegrationTest {
|
||||||
|
|
||||||
private static Logger logger = LoggerFactory.getLogger(SpringDataJPABarAuditIntegrationTest.class);
|
private static Logger logger = LoggerFactory.getLogger(SpringDataJPABarAuditIntegrationTest.class);
|
@ -1,169 +0,0 @@
|
|||||||
package com.baeldung.spring.config;
|
|
||||||
|
|
||||||
import com.baeldung.persistence.dao.IBarAuditableDao;
|
|
||||||
import com.baeldung.persistence.dao.IBarDao;
|
|
||||||
import com.baeldung.persistence.dao.IFooAuditableDao;
|
|
||||||
import com.baeldung.persistence.dao.IFooDao;
|
|
||||||
import com.baeldung.persistence.dao.impl.*;
|
|
||||||
import com.baeldung.persistence.service.IBarAuditableService;
|
|
||||||
import com.baeldung.persistence.service.IBarService;
|
|
||||||
import com.baeldung.persistence.service.IFooAuditableService;
|
|
||||||
import com.baeldung.persistence.service.IFooService;
|
|
||||||
import com.baeldung.persistence.service.impl.*;
|
|
||||||
import com.google.common.base.Preconditions;
|
|
||||||
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.context.annotation.PropertySource;
|
|
||||||
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.orm.hibernate4.HibernateTransactionManager;
|
|
||||||
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
|
|
||||||
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
|
|
||||||
@EnableTransactionManagement
|
|
||||||
@EnableJpaRepositories(basePackages = { "com.baeldung.persistence" }, transactionManagerRef = "jpaTransactionManager")
|
|
||||||
@EnableJpaAuditing
|
|
||||||
@PropertySource({ "classpath:persistence-h2.properties" })
|
|
||||||
@ComponentScan({ "com.baeldung.persistence" })
|
|
||||||
public class PersistenceTestConfig {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private Environment env;
|
|
||||||
|
|
||||||
public PersistenceTestConfig() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public LocalSessionFactoryBean sessionFactory() {
|
|
||||||
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
|
|
||||||
sessionFactory.setDataSource(restDataSource());
|
|
||||||
sessionFactory.setPackagesToScan(new String[] { "com.baeldung.persistence.model" });
|
|
||||||
sessionFactory.setHibernateProperties(hibernateProperties());
|
|
||||||
|
|
||||||
return sessionFactory;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
|
||||||
final LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
|
|
||||||
emf.setDataSource(restDataSource());
|
|
||||||
emf.setPackagesToScan(new String[] { "com.baeldung.persistence.model" });
|
|
||||||
|
|
||||||
final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
|
||||||
emf.setJpaVendorAdapter(vendorAdapter);
|
|
||||||
emf.setJpaProperties(hibernateProperties());
|
|
||||||
|
|
||||||
return emf;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public DataSource restDataSource() {
|
|
||||||
final BasicDataSource dataSource = new BasicDataSource();
|
|
||||||
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 hibernateTransactionManager() {
|
|
||||||
final HibernateTransactionManager transactionManager = new HibernateTransactionManager();
|
|
||||||
transactionManager.setSessionFactory(sessionFactory().getObject());
|
|
||||||
return transactionManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public PlatformTransactionManager jpaTransactionManager() {
|
|
||||||
final JpaTransactionManager transactionManager = new JpaTransactionManager();
|
|
||||||
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
|
|
||||||
return transactionManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
|
|
||||||
return new PersistenceExceptionTranslationPostProcessor();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public IBarService barJpaService() {
|
|
||||||
return new BarJpaService();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public IBarService barSpringDataJpaService() {
|
|
||||||
return new BarSpringDataJpaService();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public IFooService fooHibernateService() {
|
|
||||||
return new FooService();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public IBarAuditableService barHibernateAuditableService() {
|
|
||||||
return new BarAuditableService();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public IFooAuditableService fooHibernateAuditableService() {
|
|
||||||
return new FooAuditableService();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public IBarDao barJpaDao() {
|
|
||||||
return new BarJpaDao();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public IBarDao barHibernateDao() {
|
|
||||||
return new BarDao();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public IBarAuditableDao barHibernateAuditableDao() {
|
|
||||||
return new BarAuditableDao();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public IFooDao fooHibernateDao() {
|
|
||||||
return new FooDao();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public IFooAuditableDao fooHibernateAuditableDao() {
|
|
||||||
return new FooAuditableDao();
|
|
||||||
}
|
|
||||||
|
|
||||||
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,4 +1,4 @@
|
|||||||
package org.baeldung.util;
|
package com.baeldung.util;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
@ -1,58 +0,0 @@
|
|||||||
package org.baeldung.repository;
|
|
||||||
|
|
||||||
import org.baeldung.config.H2JpaConfig;
|
|
||||||
import org.baeldung.persistence.model.User;
|
|
||||||
import org.junit.After;
|
|
||||||
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.test.context.junit4.SpringRunner;
|
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
|
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by adam.
|
|
||||||
*/
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest(classes = H2JpaConfig.class)
|
|
||||||
public class UserRepositoryIntegrationTest {
|
|
||||||
|
|
||||||
private final String USER_NAME_ADAM = "Adam";
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private UserRepository userRepository;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Transactional
|
|
||||||
public void givenUsersWithSameNameInDBWhenFindAllByNameThenReturnStreamOfUsers() {
|
|
||||||
User user1 = new User();
|
|
||||||
user1.setName(USER_NAME_ADAM);
|
|
||||||
userRepository.save(user1);
|
|
||||||
|
|
||||||
User user2 = new User();
|
|
||||||
user2.setName(USER_NAME_ADAM);
|
|
||||||
userRepository.save(user2);
|
|
||||||
|
|
||||||
User user3 = new User();
|
|
||||||
user3.setName(USER_NAME_ADAM);
|
|
||||||
userRepository.save(user3);
|
|
||||||
|
|
||||||
User user4 = new User();
|
|
||||||
user4.setName("SAMPLE");
|
|
||||||
userRepository.save(user4);
|
|
||||||
|
|
||||||
try (Stream<User> foundUsersStream = userRepository.findAllByName(USER_NAME_ADAM)) {
|
|
||||||
assertThat(foundUsersStream.count()).isEqualTo(3l);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@After
|
|
||||||
public void cleanUp() {
|
|
||||||
userRepository.deleteAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user