From 28d74a520f95b29c9a9e4b74f566b251d46c7a98 Mon Sep 17 00:00:00 2001 From: amit2103 Date: Sun, 19 Aug 2018 23:32:13 +0530 Subject: [PATCH] [BAEL-8416] - Moved code from spring-boot to spring-boot-persistence module for persistence specific articles --- spring-boot-persistence/README.MD | 2 + .../com/baeldung/boot/config/H2JpaConfig.java | 67 +++++++++++++++++++ .../java/com/baeldung/domain/Country.java | 36 ++++++++++ .../baeldung/boot/domain/GenericEntity.java | 0 .../repository/GenericEntityRepository.java | 0 .../src/main/resources/application.properties | 2 + .../src/main/resources/data.sql | 10 +++ .../persistence-generic-entity.properties | 8 +++ .../src/main/resources/schema.sql | 15 +++++ .../baeldung/SpringBootH2IntegrationTest.java | 10 +-- .../SpringBootJPAIntegrationTest.java | 27 ++++++++ .../SpringBootProfileIntegrationTest.java | 9 ++- .../config/H2TestProfileJPAConfig.java | 4 +- .../src/test/resources/application.properties | 2 +- spring-boot/README.MD | 2 - 15 files changed, 179 insertions(+), 15 deletions(-) create mode 100644 spring-boot-persistence/src/main/java/com/baeldung/boot/config/H2JpaConfig.java create mode 100644 spring-boot-persistence/src/main/java/com/baeldung/domain/Country.java rename {spring-boot => spring-boot-persistence}/src/main/java/org/baeldung/boot/domain/GenericEntity.java (100%) rename {spring-boot => spring-boot-persistence}/src/main/java/org/baeldung/boot/repository/GenericEntityRepository.java (100%) create mode 100644 spring-boot-persistence/src/main/resources/data.sql create mode 100644 spring-boot-persistence/src/main/resources/persistence-generic-entity.properties create mode 100644 spring-boot-persistence/src/main/resources/schema.sql rename {spring-boot/src/test/java/org => spring-boot-persistence/src/test/java/com}/baeldung/SpringBootH2IntegrationTest.java (91%) create mode 100644 spring-boot-persistence/src/test/java/com/baeldung/SpringBootJPAIntegrationTest.java rename {spring-boot/src/test/java/org => spring-boot-persistence/src/test/java/com}/baeldung/SpringBootProfileIntegrationTest.java (95%) rename {spring-boot => spring-boot-persistence}/src/test/java/org/baeldung/config/H2TestProfileJPAConfig.java (94%) diff --git a/spring-boot-persistence/README.MD b/spring-boot-persistence/README.MD index 3fe6eb62c8..72fdca74fa 100644 --- a/spring-boot-persistence/README.MD +++ b/spring-boot-persistence/README.MD @@ -1,3 +1,5 @@ ### Relevant Articles: - [Spring Boot with Multiple SQL Import Files](http://www.baeldung.com/spring-boot-sql-import-files) +- [Configuring Separate Spring DataSource for Tests](http://www.baeldung.com/spring-testing-separate-data-source) +- [Quick Guide on data.sql and schema.sql Files in Spring Boot](http://www.baeldung.com/spring-boot-data-sql-and-schema-sql) diff --git a/spring-boot-persistence/src/main/java/com/baeldung/boot/config/H2JpaConfig.java b/spring-boot-persistence/src/main/java/com/baeldung/boot/config/H2JpaConfig.java new file mode 100644 index 0000000000..ef90714347 --- /dev/null +++ b/spring-boot-persistence/src/main/java/com/baeldung/boot/config/H2JpaConfig.java @@ -0,0 +1,67 @@ +package com.baeldung.boot.config; + +import java.util.Properties; + +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; + +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.boot.repository", "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(new String[] { "org.baeldung.boot.domain" }); + 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; + } + +} diff --git a/spring-boot-persistence/src/main/java/com/baeldung/domain/Country.java b/spring-boot-persistence/src/main/java/com/baeldung/domain/Country.java new file mode 100644 index 0000000000..e6a88c7121 --- /dev/null +++ b/spring-boot-persistence/src/main/java/com/baeldung/domain/Country.java @@ -0,0 +1,36 @@ +package com.baeldung.domain; + +import static javax.persistence.GenerationType.IDENTITY; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +public class Country { + + @Id + @GeneratedValue(strategy = IDENTITY) + private Integer id; + + @Column(nullable = false) + private String name; + + 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; + } + +} diff --git a/spring-boot/src/main/java/org/baeldung/boot/domain/GenericEntity.java b/spring-boot-persistence/src/main/java/org/baeldung/boot/domain/GenericEntity.java similarity index 100% rename from spring-boot/src/main/java/org/baeldung/boot/domain/GenericEntity.java rename to spring-boot-persistence/src/main/java/org/baeldung/boot/domain/GenericEntity.java diff --git a/spring-boot/src/main/java/org/baeldung/boot/repository/GenericEntityRepository.java b/spring-boot-persistence/src/main/java/org/baeldung/boot/repository/GenericEntityRepository.java similarity index 100% rename from spring-boot/src/main/java/org/baeldung/boot/repository/GenericEntityRepository.java rename to spring-boot-persistence/src/main/java/org/baeldung/boot/repository/GenericEntityRepository.java diff --git a/spring-boot-persistence/src/main/resources/application.properties b/spring-boot-persistence/src/main/resources/application.properties index e69de29bb2..d0fb785fe8 100644 --- a/spring-boot-persistence/src/main/resources/application.properties +++ b/spring-boot-persistence/src/main/resources/application.properties @@ -0,0 +1,2 @@ +spring.jpa.show-sql=true +spring.jpa.hibernate.ddl-auto=none \ No newline at end of file diff --git a/spring-boot-persistence/src/main/resources/data.sql b/spring-boot-persistence/src/main/resources/data.sql new file mode 100644 index 0000000000..feef02b6cf --- /dev/null +++ b/spring-boot-persistence/src/main/resources/data.sql @@ -0,0 +1,10 @@ +insert into users values (1, 'Alex', 1); +insert into users values (2, 'Bob', 1); +insert into users values (3, 'John', 0); +insert into users values (4, 'Harry', 0); +insert into users values (5, 'Smith', 1); + +INSERT INTO country (name) VALUES ('India'); +INSERT INTO country (name) VALUES ('Brazil'); +INSERT INTO country (name) VALUES ('USA'); +INSERT INTO country (name) VALUES ('Italy'); \ No newline at end of file diff --git a/spring-boot-persistence/src/main/resources/persistence-generic-entity.properties b/spring-boot-persistence/src/main/resources/persistence-generic-entity.properties new file mode 100644 index 0000000000..b19304cb1f --- /dev/null +++ b/spring-boot-persistence/src/main/resources/persistence-generic-entity.properties @@ -0,0 +1,8 @@ +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 diff --git a/spring-boot-persistence/src/main/resources/schema.sql b/spring-boot-persistence/src/main/resources/schema.sql new file mode 100644 index 0000000000..4cfc8a7927 --- /dev/null +++ b/spring-boot-persistence/src/main/resources/schema.sql @@ -0,0 +1,15 @@ +drop table if exists USERS; +drop table if exists country; + +create table USERS( + ID int not null AUTO_INCREMENT, + NAME varchar(100) not null, + STATUS int, + PRIMARY KEY ( ID ) +); + +CREATE TABLE country ( + id INTEGER NOT NULL AUTO_INCREMENT, + name VARCHAR(128) NOT NULL, + PRIMARY KEY (id) +); \ No newline at end of file diff --git a/spring-boot/src/test/java/org/baeldung/SpringBootH2IntegrationTest.java b/spring-boot-persistence/src/test/java/com/baeldung/SpringBootH2IntegrationTest.java similarity index 91% rename from spring-boot/src/test/java/org/baeldung/SpringBootH2IntegrationTest.java rename to spring-boot-persistence/src/test/java/com/baeldung/SpringBootH2IntegrationTest.java index 290cfbe081..6479e90113 100644 --- a/spring-boot/src/test/java/org/baeldung/SpringBootH2IntegrationTest.java +++ b/spring-boot-persistence/src/test/java/com/baeldung/SpringBootH2IntegrationTest.java @@ -1,7 +1,8 @@ -package org.baeldung; +package com.baeldung; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; -import org.baeldung.boot.Application; -import org.baeldung.boot.config.H2JpaConfig; import org.baeldung.boot.domain.GenericEntity; import org.baeldung.boot.repository.GenericEntityRepository; import org.junit.Test; @@ -10,8 +11,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import com.baeldung.boot.config.H2JpaConfig; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = { Application.class, H2JpaConfig.class }) diff --git a/spring-boot-persistence/src/test/java/com/baeldung/SpringBootJPAIntegrationTest.java b/spring-boot-persistence/src/test/java/com/baeldung/SpringBootJPAIntegrationTest.java new file mode 100644 index 0000000000..eef9ebe953 --- /dev/null +++ b/spring-boot-persistence/src/test/java/com/baeldung/SpringBootJPAIntegrationTest.java @@ -0,0 +1,27 @@ +package com.baeldung; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.baeldung.boot.domain.GenericEntity; +import org.baeldung.boot.repository.GenericEntityRepository; +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; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Application.class) +public class SpringBootJPAIntegrationTest { + @Autowired + private GenericEntityRepository genericEntityRepository; + + @Test + public void givenGenericEntityRepository_whenSaveAndRetreiveEntity_thenOK() { + GenericEntity genericEntity = genericEntityRepository.save(new GenericEntity("test")); + GenericEntity foundEntity = genericEntityRepository.findById(genericEntity.getId()).orElse(null); + assertNotNull(foundEntity); + assertEquals(genericEntity.getValue(), foundEntity.getValue()); + } +} \ No newline at end of file diff --git a/spring-boot/src/test/java/org/baeldung/SpringBootProfileIntegrationTest.java b/spring-boot-persistence/src/test/java/com/baeldung/SpringBootProfileIntegrationTest.java similarity index 95% rename from spring-boot/src/test/java/org/baeldung/SpringBootProfileIntegrationTest.java rename to spring-boot-persistence/src/test/java/com/baeldung/SpringBootProfileIntegrationTest.java index 128a05f103..4c68f1d4a0 100644 --- a/spring-boot/src/test/java/org/baeldung/SpringBootProfileIntegrationTest.java +++ b/spring-boot-persistence/src/test/java/com/baeldung/SpringBootProfileIntegrationTest.java @@ -1,6 +1,8 @@ -package org.baeldung; +package com.baeldung; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; -import org.baeldung.boot.Application; import org.baeldung.boot.domain.GenericEntity; import org.baeldung.boot.repository.GenericEntityRepository; import org.baeldung.config.H2TestProfileJPAConfig; @@ -11,9 +13,6 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - @RunWith(SpringRunner.class) @SpringBootTest(classes = { Application.class, H2TestProfileJPAConfig.class }) @ActiveProfiles("test") diff --git a/spring-boot/src/test/java/org/baeldung/config/H2TestProfileJPAConfig.java b/spring-boot-persistence/src/test/java/org/baeldung/config/H2TestProfileJPAConfig.java similarity index 94% rename from spring-boot/src/test/java/org/baeldung/config/H2TestProfileJPAConfig.java rename to spring-boot-persistence/src/test/java/org/baeldung/config/H2TestProfileJPAConfig.java index d0b92a7a93..7f962e1417 100644 --- a/spring-boot/src/test/java/org/baeldung/config/H2TestProfileJPAConfig.java +++ b/spring-boot-persistence/src/test/java/org/baeldung/config/H2TestProfileJPAConfig.java @@ -18,7 +18,7 @@ import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration -@EnableJpaRepositories(basePackages = { "org.baeldung.repository", "org.baeldung.boot.repository", "org.baeldung.boot.boottest" }) +@EnableJpaRepositories(basePackages = { "org.baeldung.repository", "org.baeldung.boot.repository" }) @EnableTransactionManagement public class H2TestProfileJPAConfig { @@ -41,7 +41,7 @@ public class H2TestProfileJPAConfig { public LocalContainerEntityManagerFactoryBean entityManagerFactory() { final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource()); - em.setPackagesToScan(new String[] { "org.baeldung.domain", "org.baeldung.boot.domain", "org.baeldung.boot.boottest", "org.baeldung.model" }); + em.setPackagesToScan(new String[] { "org.baeldung.domain", "org.baeldung.boot.domain" }); em.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); em.setJpaProperties(additionalProperties()); return em; diff --git a/spring-boot-persistence/src/test/resources/application.properties b/spring-boot-persistence/src/test/resources/application.properties index a5d09db840..a5c1d983cf 100644 --- a/spring-boot-persistence/src/test/resources/application.properties +++ b/spring-boot-persistence/src/test/resources/application.properties @@ -13,4 +13,4 @@ hibernate.cache.use_query_cache=true hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory spring.jpa.properties.hibernate.hbm2ddl.import_files=migrated_users.sql -spring.datasource.data=import_*_users.sql,import_articles.sql \ No newline at end of file +spring.datasource.data=import_*_users.sql \ No newline at end of file diff --git a/spring-boot/README.MD b/spring-boot/README.MD index 1532889a5c..6892278f09 100644 --- a/spring-boot/README.MD +++ b/spring-boot/README.MD @@ -12,7 +12,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Using Custom Banners in Spring Boot](http://www.baeldung.com/spring-boot-custom-banners) - [Guide to Internationalization in Spring Boot](http://www.baeldung.com/spring-boot-internationalization) - [Create a Custom FailureAnalyzer with Spring Boot](http://www.baeldung.com/spring-boot-failure-analyzer) -- [Configuring Separate Spring DataSource for Tests](http://www.baeldung.com/spring-testing-separate-data-source) - [Dynamic DTO Validation Config Retrieved from DB](http://www.baeldung.com/spring-dynamic-dto-validation) - [Custom Information in Spring Boot Info Endpoint](http://www.baeldung.com/spring-boot-info-actuator-custom) - [Using @JsonComponent in Spring Boot](http://www.baeldung.com/spring-boot-jsoncomponent) @@ -23,7 +22,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Spring Boot and Togglz Aspect](http://www.baeldung.com/spring-togglz) - [Getting Started with GraphQL and Spring Boot](http://www.baeldung.com/spring-graphql) - [Guide to Spring Type Conversions](http://www.baeldung.com/spring-type-conversions) -- [Quick Guide on data.sql and schema.sql Files in Spring Boot](http://www.baeldung.com/spring-boot-data-sql-and-schema-sql) - [Spring Data Java 8 Support](http://www.baeldung.com/spring-data-java-8) - [An Introduction to Kong](http://www.baeldung.com/kong) - [Spring Boot Customize Whitelabel Error Page](http://www.baeldung.com/spring-boot-custom-error-page)