Created
August 31, 2013 12:19
-
-
Save altfatterz/6397839 to your computer and use it in GitHub Desktop.
InfrastructureConfig
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
package com.za.spring.fun.config; | |
import javax.sql.DataSource; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; | |
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; | |
import org.springframework.orm.jpa.JpaTransactionManager; | |
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | |
import org.springframework.orm.jpa.vendor.Database; | |
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; | |
import org.springframework.transaction.PlatformTransactionManager; | |
/** | |
* Java based Spring infrastructure configuration. | |
* | |
* @author Zoltan Altfatter | |
*/ | |
@Configuration | |
public class InfrastructureConfig { | |
@Bean | |
public DataSource dataSource() { | |
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); | |
return builder.setType(EmbeddedDatabaseType.H2).build(); | |
} | |
@Bean | |
public LocalContainerEntityManagerFactoryBean entityManagerFactory() { | |
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); | |
vendorAdapter.setDatabase(Database.H2); | |
vendorAdapter.setGenerateDdl(true); | |
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); | |
factory.setJpaVendorAdapter(vendorAdapter); | |
factory.setPackagesToScan("com.za.spring.fun"); | |
factory.setDataSource(dataSource()); | |
return factory; | |
} | |
@Bean | |
public PlatformTransactionManager transactionManager() { | |
JpaTransactionManager txManager = new JpaTransactionManager(); | |
txManager.setEntityManagerFactory(entityManagerFactory().getObject()); | |
return txManager; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment