1+ package org .baeldung .ex .mappingexception .spring ;
2+
3+ import java .util .Properties ;
4+
5+ import javax .sql .DataSource ;
6+
7+ import org .apache .tomcat .dbcp .dbcp .BasicDataSource ;
8+ import org .springframework .beans .factory .annotation .Autowired ;
9+ import org .springframework .context .annotation .Bean ;
10+ import org .springframework .context .annotation .ComponentScan ;
11+ import org .springframework .context .annotation .Configuration ;
12+ import org .springframework .context .annotation .PropertySource ;
13+ import org .springframework .core .env .Environment ;
14+ import org .springframework .dao .annotation .PersistenceExceptionTranslationPostProcessor ;
15+ import org .springframework .orm .hibernate4 .HibernateTransactionManager ;
16+ import org .springframework .orm .hibernate4 .LocalSessionFactoryBean ;
17+ import org .springframework .transaction .annotation .EnableTransactionManagement ;
18+
19+ import com .google .common .base .Preconditions ;
20+
21+ @ Configuration
22+ @ EnableTransactionManagement
23+ @ PropertySource ({ "classpath:persistence-mysql.properties" })
24+ @ ComponentScan ({ "org.baeldung.persistence" })
25+ public class PersistenceConfig {
26+
27+ @ Autowired
28+ private Environment env ;
29+
30+ public PersistenceConfig () {
31+ super ();
32+ }
33+
34+ @ Bean
35+ public LocalSessionFactoryBean sessionFactory () {
36+ final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean ();
37+ sessionFactory .setDataSource (restDataSource ());
38+ sessionFactory .setPackagesToScan (new String [] { "org.baeldung.persistence.model2" });
39+ sessionFactory .setHibernateProperties (hibernateProperties ());
40+
41+ return sessionFactory ;
42+ }
43+
44+ @ Bean
45+ public DataSource restDataSource () {
46+ final BasicDataSource dataSource = new BasicDataSource ();
47+ dataSource .setDriverClassName (Preconditions .checkNotNull (env .getProperty ("jdbc.driverClassName" )));
48+ dataSource .setUrl (Preconditions .checkNotNull (env .getProperty ("jdbc.url" )));
49+ dataSource .setUsername (Preconditions .checkNotNull (env .getProperty ("jdbc.user" )));
50+ dataSource .setPassword (Preconditions .checkNotNull (env .getProperty ("jdbc.pass" )));
51+
52+ return dataSource ;
53+ }
54+
55+ @ Bean
56+ public HibernateTransactionManager transactionManager () {
57+ final HibernateTransactionManager txManager = new HibernateTransactionManager ();
58+ txManager .setSessionFactory (sessionFactory ().getObject ());
59+
60+ return txManager ;
61+ }
62+
63+ @ Bean
64+ public PersistenceExceptionTranslationPostProcessor exceptionTranslation () {
65+ return new PersistenceExceptionTranslationPostProcessor ();
66+ }
67+
68+ final Properties hibernateProperties () {
69+ return new Properties () {
70+ {
71+ setProperty ("hibernate.hbm2ddl.auto" , env .getProperty ("hibernate.hbm2ddl.auto" ));
72+ setProperty ("hibernate.dialect" , env .getProperty ("hibernate.dialect" ));
73+
74+ // setProperty("hibernate.globally_quoted_identifiers", "true");
75+ // note: necessary in launchpad-storage, but causing problems here
76+ }
77+ };
78+ }
79+ }
0 commit comments