Skip to content
This repository was archived by the owner on Oct 21, 2024. It is now read-only.

Commit a15e874

Browse files
committed
spring-jpa: working spring jpa
deleted all unwanted hibernate code
1 parent 214831e commit a15e874

File tree

7 files changed

+129
-120
lines changed

7 files changed

+129
-120
lines changed

spring/spring-data-jpa/app/pom.xml

+16-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
<spring.version>5.1.6.RELEASE</spring.version>
2020
<spring-boot.version>2.0.0.RELEASE</spring-boot.version>
21+
<spring-data-jpa.version>2.1.8.RELEASE</spring-data-jpa.version>
22+
<datasource-hikari.version>3.3.1</datasource-hikari.version>
2123

2224
<lombok.version>1.18.6</lombok.version>
2325
<slf4j.version>1.7.25</slf4j.version>
@@ -49,7 +51,6 @@
4951
<version>${hibernate.mysql.connector}</version>
5052
</dependency>
5153

52-
5354
<!-- Spring Boot -->
5455
<dependency>
5556
<groupId>org.springframework.boot</groupId>
@@ -63,6 +64,20 @@
6364
<version>${spring-boot.version}</version>
6465
</dependency>
6566

67+
<!-- Spring Data JPA -->
68+
<dependency>
69+
<groupId>org.springframework.data</groupId>
70+
<artifactId>spring-data-jpa</artifactId>
71+
<version>${spring-data-jpa.version}</version>
72+
</dependency>
73+
74+
<!-- DataSource (HikariCP) -->
75+
<dependency>
76+
<groupId>com.zaxxer</groupId>
77+
<artifactId>HikariCP</artifactId>
78+
<version>${datasource-hikari.version}</version>
79+
</dependency>
80+
6681
<!-- Spring -->
6782
<dependency>
6883
<groupId>org.springframework</groupId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package es.msanchez.frameworks.java.spring.boot.config;
2+
3+
import com.zaxxer.hikari.HikariConfig;
4+
import com.zaxxer.hikari.HikariDataSource;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.core.env.Environment;
8+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
9+
import org.springframework.orm.jpa.JpaTransactionManager;
10+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
11+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
12+
import org.springframework.transaction.annotation.EnableTransactionManagement;
13+
14+
import javax.persistence.EntityManagerFactory;
15+
import javax.sql.DataSource;
16+
import java.util.Properties;
17+
18+
@Configuration
19+
@EnableJpaRepositories(basePackages = {
20+
"es.msanchez.frameworks.java.spring.boot.**"
21+
})
22+
@EnableTransactionManagement
23+
public class DatabaseConfig {
24+
25+
@Bean
26+
DataSource dataSource(final Environment env) {
27+
final HikariConfig dataSourceConfig = new HikariConfig();
28+
dataSourceConfig.setDriverClassName(env.getRequiredProperty("db.driver"));
29+
dataSourceConfig.setJdbcUrl(env.getRequiredProperty("db.url"));
30+
dataSourceConfig.setUsername(env.getRequiredProperty("db.username"));
31+
dataSourceConfig.setPassword(env.getRequiredProperty("db.password"));
32+
return new HikariDataSource(dataSourceConfig);
33+
}
34+
35+
@Bean
36+
LocalContainerEntityManagerFactoryBean entityManagerFactory(final DataSource dataSource,
37+
final Environment env) {
38+
final LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
39+
entityManagerFactoryBean.setDataSource(dataSource);
40+
entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
41+
entityManagerFactoryBean.setPackagesToScan("es.msanchez.frameworks.java.spring.boot.**");
42+
43+
final Properties jpaProperties = new Properties();
44+
45+
//Configures the used database dialect. This allows Hibernate to create SQL
46+
//that is optimized for the used database.
47+
jpaProperties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect"));
48+
49+
//Specifies the action that is invoked to the database when the Hibernate
50+
//SessionFactory is created or closed.
51+
jpaProperties.put("hibernate.hbm2ddl.auto",
52+
env.getRequiredProperty("hibernate.hbm2ddl.auto")
53+
);
54+
55+
//Configures the naming strategy that is used when Hibernate creates
56+
//new database objects and schema elements
57+
jpaProperties.put("hibernate.ejb.naming_strategy",
58+
env.getRequiredProperty("hibernate.ejb.naming_strategy")
59+
);
60+
61+
//If the value of this property is true, Hibernate writes all SQL
62+
//statements to the console.
63+
jpaProperties.put("hibernate.show_sql",
64+
env.getRequiredProperty("hibernate.show_sql")
65+
);
66+
67+
//If the value of this property is true, Hibernate will format the SQL
68+
//that is written to the console.
69+
jpaProperties.put("hibernate.format_sql",
70+
env.getRequiredProperty("hibernate.format_sql")
71+
);
72+
73+
entityManagerFactoryBean.setJpaProperties(jpaProperties);
74+
75+
return entityManagerFactoryBean;
76+
}
77+
78+
@Bean
79+
JpaTransactionManager transactionManager(final EntityManagerFactory entityManagerFactory) {
80+
final JpaTransactionManager transactionManager = new JpaTransactionManager();
81+
transactionManager.setEntityManagerFactory(entityManagerFactory);
82+
return transactionManager;
83+
}
84+
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package es.msanchez.frameworks.java.spring.boot.config;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
@ComponentScan(basePackages = {
8+
"es.msanchez.frameworks.java.spring.boot.**"
9+
})
10+
public class SpringConfig {
11+
}

spring/spring-data-jpa/app/src/main/java/es/msanchez/frameworks/java/spring/boot/hibernate/HibernateUtil.java

-27
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Spring boot Configuration
2+
3+
# 0 = random port
4+
server.port=0
5+
6+
# Database Configuration
7+
db.driver=com.mysql.jdbc.Driver
8+
db.url=jdbc:mysql://localhost:3310/hibernate
9+
db.username=hibernate_user
10+
db.password=hibernate_pass
11+
12+
# Hibernate Configuration
13+
hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
14+
hibernate.hbm2ddl.auto=create-drop
15+
hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
16+
hibernate.show_sql=true
17+
hibernate.format_sql=true

spring/spring-data-jpa/app/src/main/resources/hibernate.cfg.xml

-26
This file was deleted.

spring/spring-data-jpa/app/src/test/java/es/msanchez/frameworks/java/spring/boot/hibernate/HibernateUtilTest.java

-66
This file was deleted.

0 commit comments

Comments
 (0)