Created
March 20, 2015 19:19
-
-
Save philwebb/ab6ad596a0bc334bb70a to your computer and use it in GitHub Desktop.
multids
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 sample; | |
import javax.persistence.EntityManagerFactory; | |
import javax.sql.DataSource; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Qualifier; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilder; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Primary; | |
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.transaction.PlatformTransactionManager; | |
@SpringBootApplication | |
public class Application { | |
// + spring.jpa.open-in-view=false in application.properties | |
@Autowired | |
private EntityManagerFactoryBuilder factoryBuilder; | |
@Bean | |
@Primary | |
public DataSource customerDataSource() { | |
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build(); | |
} | |
@Bean | |
public DataSource stockDataSource() { | |
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL).build(); | |
} | |
@Bean | |
public LocalContainerEntityManagerFactoryBean customerEntityManagerFactory() { | |
return factoryBuilder.dataSource(customerDataSource()) | |
.packages("sample.customer").build(); | |
} | |
@Bean | |
public LocalContainerEntityManagerFactoryBean stockEntityManagerFactory() { | |
return factoryBuilder.dataSource(customerDataSource()).packages("sample.stock") | |
.build(); | |
} | |
@Bean | |
public PlatformTransactionManager transactionManager( | |
@Qualifier("customerEntityManagerFactory") EntityManagerFactory factory) { | |
return new JpaTransactionManager(factory); | |
} | |
public static void main(String[] args) { | |
SpringApplication.run(Application.class, args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment