1
+ package com .baeldung .spring ;
2
+
3
+ import java .util .Properties ;
4
+
5
+ import javax .sql .DataSource ;
6
+
7
+ import org .springframework .beans .factory .annotation .Autowired ;
8
+ import org .springframework .context .annotation .Bean ;
9
+ import org .springframework .context .annotation .ComponentScan ;
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 .dao .annotation .PersistenceExceptionTranslationPostProcessor ;
14
+ import org .springframework .data .jpa .repository .config .EnableJpaRepositories ;
15
+ import org .springframework .jdbc .datasource .DriverManagerDataSource ;
16
+ import org .springframework .orm .jpa .JpaTransactionManager ;
17
+ import org .springframework .orm .jpa .LocalContainerEntityManagerFactoryBean ;
18
+ import org .springframework .orm .jpa .vendor .HibernateJpaVendorAdapter ;
19
+ import org .springframework .transaction .PlatformTransactionManager ;
20
+ import org .springframework .transaction .annotation .EnableTransactionManagement ;
21
+
22
+ import com .google .common .base .Preconditions ;
23
+
24
+ @ Configuration
25
+ @ EnableTransactionManagement
26
+ @ PropertySource ({ "classpath:persistence-${envTarget:h2}.properties" })
27
+ @ ComponentScan ({ "com.baeldung.persistence" })
28
+ // @ImportResource("classpath*:springDataPersistenceConfig.xml")
29
+ @ EnableJpaRepositories (basePackages = "com.baeldung.persistence.dao" )
30
+ public class PersistenceConfig {
31
+
32
+ @ Autowired
33
+ private Environment env ;
34
+
35
+ public PersistenceConfig () {
36
+ super ();
37
+ }
38
+
39
+ @ Bean
40
+ public LocalContainerEntityManagerFactoryBean entityManagerFactory () {
41
+ final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean ();
42
+ em .setDataSource (dataSource ());
43
+ em .setPackagesToScan (new String [] { "com.baeldung.persistence.model" });
44
+
45
+ final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter ();
46
+ // vendorAdapter.set
47
+ em .setJpaVendorAdapter (vendorAdapter );
48
+ em .setJpaProperties (additionalProperties ());
49
+
50
+ return em ;
51
+ }
52
+
53
+ @ Bean
54
+ public DataSource dataSource () {
55
+ final DriverManagerDataSource dataSource = new DriverManagerDataSource ();
56
+ dataSource .setDriverClassName (Preconditions .checkNotNull (env .getProperty ("jdbc.driverClassName" )));
57
+ dataSource .setUrl (Preconditions .checkNotNull (env .getProperty ("jdbc.url" )));
58
+ dataSource .setUsername (Preconditions .checkNotNull (env .getProperty ("jdbc.user" )));
59
+ dataSource .setPassword (Preconditions .checkNotNull (env .getProperty ("jdbc.pass" )));
60
+
61
+ return dataSource ;
62
+ }
63
+
64
+ @ Bean
65
+ public PlatformTransactionManager transactionManager () {
66
+ final JpaTransactionManager transactionManager = new JpaTransactionManager ();
67
+ transactionManager .setEntityManagerFactory (entityManagerFactory ().getObject ());
68
+
69
+ return transactionManager ;
70
+ }
71
+
72
+ @ Bean
73
+ public PersistenceExceptionTranslationPostProcessor exceptionTranslation () {
74
+ return new PersistenceExceptionTranslationPostProcessor ();
75
+ }
76
+
77
+ final Properties additionalProperties () {
78
+ final Properties hibernateProperties = new Properties ();
79
+ hibernateProperties .setProperty ("hibernate.hbm2ddl.auto" , env .getProperty ("hibernate.hbm2ddl.auto" ));
80
+ hibernateProperties .setProperty ("hibernate.dialect" , env .getProperty ("hibernate.dialect" ));
81
+ // hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
82
+ return hibernateProperties ;
83
+ }
84
+
85
+ }
0 commit comments