Skip to content

Commit 28d74a5

Browse files
committed
[BAEL-8416] - Moved code from spring-boot to spring-boot-persistence module for persistence specific articles
1 parent 110a74e commit 28d74a5

15 files changed

Lines changed: 179 additions & 15 deletions

File tree

‎spring-boot-persistence/README.MD‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
### Relevant Articles:
22

33
- [Spring Boot with Multiple SQL Import Files](http://www.baeldung.com/spring-boot-sql-import-files)
4+
- [Configuring Separate Spring DataSource for Tests](http://www.baeldung.com/spring-testing-separate-data-source)
5+
- [Quick Guide on data.sql and schema.sql Files in Spring Boot](http://www.baeldung.com/spring-boot-data-sql-and-schema-sql)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.baeldung.boot.config;
2+
3+
import java.util.Properties;
4+
5+
import javax.persistence.EntityManagerFactory;
6+
import javax.sql.DataSource;
7+
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.context.annotation.PropertySource;
12+
import org.springframework.core.env.Environment;
13+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
14+
import org.springframework.jdbc.datasource.DriverManagerDataSource;
15+
import org.springframework.orm.jpa.JpaTransactionManager;
16+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
17+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
18+
import org.springframework.transaction.annotation.EnableTransactionManagement;
19+
20+
@Configuration
21+
@EnableJpaRepositories(basePackages = { "org.baeldung.boot.repository", "org.baeldung.repository" })
22+
@PropertySource("classpath:persistence-generic-entity.properties")
23+
@EnableTransactionManagement
24+
public class H2JpaConfig {
25+
26+
@Autowired
27+
private Environment env;
28+
29+
@Bean
30+
public DataSource dataSource() {
31+
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
32+
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
33+
dataSource.setUrl(env.getProperty("jdbc.url"));
34+
dataSource.setUsername(env.getProperty("jdbc.user"));
35+
dataSource.setPassword(env.getProperty("jdbc.pass"));
36+
37+
return dataSource;
38+
}
39+
40+
@Bean
41+
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
42+
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
43+
em.setDataSource(dataSource());
44+
em.setPackagesToScan(new String[] { "org.baeldung.boot.domain" });
45+
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
46+
em.setJpaProperties(additionalProperties());
47+
return em;
48+
}
49+
50+
@Bean
51+
JpaTransactionManager transactionManager(final EntityManagerFactory entityManagerFactory) {
52+
final JpaTransactionManager transactionManager = new JpaTransactionManager();
53+
transactionManager.setEntityManagerFactory(entityManagerFactory);
54+
return transactionManager;
55+
}
56+
57+
final Properties additionalProperties() {
58+
final Properties hibernateProperties = new Properties();
59+
60+
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
61+
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
62+
hibernateProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
63+
64+
return hibernateProperties;
65+
}
66+
67+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.domain;
2+
3+
import static javax.persistence.GenerationType.IDENTITY;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.Id;
9+
10+
@Entity
11+
public class Country {
12+
13+
@Id
14+
@GeneratedValue(strategy = IDENTITY)
15+
private Integer id;
16+
17+
@Column(nullable = false)
18+
private String name;
19+
20+
public Integer getId() {
21+
return id;
22+
}
23+
24+
public void setId(Integer id) {
25+
this.id = id;
26+
}
27+
28+
public String getName() {
29+
return name;
30+
}
31+
32+
public void setName(String name) {
33+
this.name = name;
34+
}
35+
36+
}

spring-boot/src/main/java/org/baeldung/boot/domain/GenericEntity.java renamed to spring-boot-persistence/src/main/java/org/baeldung/boot/domain/GenericEntity.java

File renamed without changes.

spring-boot/src/main/java/org/baeldung/boot/repository/GenericEntityRepository.java renamed to spring-boot-persistence/src/main/java/org/baeldung/boot/repository/GenericEntityRepository.java

File renamed without changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
spring.jpa.show-sql=true
2+
spring.jpa.hibernate.ddl-auto=none
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
insert into users values (1, 'Alex', 1);
2+
insert into users values (2, 'Bob', 1);
3+
insert into users values (3, 'John', 0);
4+
insert into users values (4, 'Harry', 0);
5+
insert into users values (5, 'Smith', 1);
6+
7+
INSERT INTO country (name) VALUES ('India');
8+
INSERT INTO country (name) VALUES ('Brazil');
9+
INSERT INTO country (name) VALUES ('USA');
10+
INSERT INTO country (name) VALUES ('Italy');
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
jdbc.driverClassName=org.h2.Driver
2+
jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
3+
jdbc.user=sa
4+
jdbc.pass=sa
5+
6+
hibernate.dialect=org.hibernate.dialect.H2Dialect
7+
hibernate.show_sql=true
8+
hibernate.hbm2ddl.auto=create-drop
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
drop table if exists USERS;
2+
drop table if exists country;
3+
4+
create table USERS(
5+
ID int not null AUTO_INCREMENT,
6+
NAME varchar(100) not null,
7+
STATUS int,
8+
PRIMARY KEY ( ID )
9+
);
10+
11+
CREATE TABLE country (
12+
id INTEGER NOT NULL AUTO_INCREMENT,
13+
name VARCHAR(128) NOT NULL,
14+
PRIMARY KEY (id)
15+
);

spring-boot/src/test/java/org/baeldung/SpringBootH2IntegrationTest.java renamed to spring-boot-persistence/src/test/java/com/baeldung/SpringBootH2IntegrationTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
package org.baeldung;
1+
package com.baeldung;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
25

3-
import org.baeldung.boot.Application;
4-
import org.baeldung.boot.config.H2JpaConfig;
56
import org.baeldung.boot.domain.GenericEntity;
67
import org.baeldung.boot.repository.GenericEntityRepository;
78
import org.junit.Test;
@@ -10,8 +11,7 @@
1011
import org.springframework.boot.test.context.SpringBootTest;
1112
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
1213

13-
import static org.junit.Assert.assertEquals;
14-
import static org.junit.Assert.assertNotNull;
14+
import com.baeldung.boot.config.H2JpaConfig;
1515

1616
@RunWith(SpringJUnit4ClassRunner.class)
1717
@SpringBootTest(classes = { Application.class, H2JpaConfig.class })

0 commit comments

Comments
 (0)