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