[BAEL-8416] - Moved code from spring-boot to spring-boot-persistence module for persistence specific articles
This commit is contained in:
parent
110a74e4b1
commit
28d74a520f
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
spring.jpa.show-sql=true
|
||||
spring.jpa.hibernate.ddl-auto=none
|
|
@ -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');
|
|
@ -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
|
|
@ -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)
|
||||
);
|
|
@ -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 })
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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")
|
|
@ -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;
|
|
@ -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
|
||||
spring.datasource.data=import_*_users.sql
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue