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