Created
November 30, 2013 01:45
-
-
Save Qkyrie/7714379 to your computer and use it in GitHub Desktop.
Spring JPAConfiguration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.springframework.beans.factory.annotation.Value; | |
| import org.springframework.context.annotation.Bean; | |
| import org.springframework.context.annotation.Configuration; | |
| import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | |
| import org.springframework.jdbc.datasource.DriverManagerDataSource; | |
| import org.springframework.orm.jpa.JpaTransactionManager; | |
| import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | |
| import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; | |
| import org.springframework.transaction.PlatformTransactionManager; | |
| import org.springframework.transaction.annotation.EnableTransactionManagement; | |
| import org.springframework.transaction.annotation.TransactionManagementConfigurer; | |
| import javax.sql.DataSource; | |
| import java.util.Properties; | |
| /** | |
| * User: Quinten | |
| * Date: 28-11-13 | |
| * Time: 20:48 | |
| * | |
| * @author Quinten De Swaef | |
| */ | |
| @Configuration | |
| @EnableTransactionManagement | |
| @EnableJpaRepositories(basePackageClasses = Application.class) | |
| class JpaConfiguration implements TransactionManagementConfigurer { | |
| @Value("${dataSource.driverClassName}") | |
| private String driver; | |
| @Value("${dataSource.url}") | |
| private String url; | |
| @Value("${dataSource.username}") | |
| private String username; | |
| @Value("${dataSource.password}") | |
| private String password; | |
| @Value("${hibernate.dialect}") | |
| private String dialect; | |
| @Value("${hibernate.hbm2ddl.auto}") | |
| private String hbm2ddlAuto; | |
| @Bean | |
| public DataSource configureDataSource() { | |
| DriverManagerDataSource dataSource = new DriverManagerDataSource(); | |
| dataSource.setDriverClassName(driver); | |
| dataSource.setUrl(url); | |
| dataSource.setUsername(username); | |
| dataSource.setPassword(password); | |
| return dataSource; | |
| } | |
| @Bean | |
| public LocalContainerEntityManagerFactoryBean configureEntityManagerFactory() { | |
| LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); | |
| entityManagerFactoryBean.setDataSource(configureDataSource()); | |
| entityManagerFactoryBean.setPackagesToScan("${package}"); | |
| entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); | |
| Properties jpaProperties = new Properties(); | |
| jpaProperties.put(org.hibernate.cfg.Environment.DIALECT, dialect); | |
| jpaProperties.put(org.hibernate.cfg.Environment.HBM2DDL_AUTO, hbm2ddlAuto); | |
| entityManagerFactoryBean.setJpaProperties(jpaProperties); | |
| return entityManagerFactoryBean; | |
| } | |
| @Bean | |
| public PlatformTransactionManager annotationDrivenTransactionManager() { | |
| return new JpaTransactionManager(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment