Skip to content

Commit

Permalink
Use distinct JPA properties
Browse files Browse the repository at this point in the history
Upgrade the sample to disable the JPA auto-configuration altogether and
use two different sets of JPA configuration.

This involve a bit of copy/paste for now but maybe we can improve that in
Spring Boot itself.
  • Loading branch information
snicoll committed Aug 7, 2015
1 parent 9e8ed95 commit b585747
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/main/java/demo/DemoMultiEntityManagers.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;

@SpringBootApplication
@SpringBootApplication(exclude = HibernateJpaAutoConfiguration.class)
public class DemoMultiEntityManagers {

public static void main(String[] args) {
Expand Down
33 changes: 32 additions & 1 deletion src/main/java/demo/customer/CustomerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@

import demo.customer.domain.Customer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

@Configuration
@EnableJpaRepositories(
Expand All @@ -22,6 +28,14 @@
basePackageClasses = Customer.class)
public class CustomerConfig {

@Autowired(required = false)
private PersistenceUnitManager persistenceUnitManager;

@Bean
@ConfigurationProperties("app.customer.jpa")
public JpaProperties customerJpaProperties() {
return new JpaProperties();
}

@Bean
@Primary
Expand All @@ -32,7 +46,8 @@ public DataSource customerDataSource() {

@Bean
public LocalContainerEntityManagerFactoryBean customerEntityManager(
EntityManagerFactoryBuilder builder) {
JpaProperties customerJpaProperties) {
EntityManagerFactoryBuilder builder = createEntityManagerFactoryBuilder(customerJpaProperties);
return builder
.dataSource(customerDataSource())
.packages(Customer.class)
Expand All @@ -45,4 +60,20 @@ public LocalContainerEntityManagerFactoryBean customerEntityManager(
public JpaTransactionManager customerTransactionManager(EntityManagerFactory customerEntityManager) {
return new JpaTransactionManager(customerEntityManager);
}

private EntityManagerFactoryBuilder createEntityManagerFactoryBuilder(JpaProperties customerJpaProperties) {
JpaVendorAdapter jpaVendorAdapter = createJpaVendorAdapter(customerJpaProperties);
return new EntityManagerFactoryBuilder(
jpaVendorAdapter, customerJpaProperties, this.persistenceUnitManager);
}

private JpaVendorAdapter createJpaVendorAdapter(JpaProperties jpaProperties) {
AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setShowSql(jpaProperties.isShowSql());
adapter.setDatabase(jpaProperties.getDatabase());
adapter.setDatabasePlatform(jpaProperties.getDatabasePlatform());
adapter.setGenerateDdl(jpaProperties.isGenerateDdl());
return adapter;
}

}
33 changes: 32 additions & 1 deletion src/main/java/demo/order/OrderConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@

import demo.order.domain.Order;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

@Configuration
@EnableJpaRepositories(
Expand All @@ -21,6 +27,15 @@
basePackageClasses = Order.class)
public class OrderConfig {

@Autowired(required = false)
private PersistenceUnitManager persistenceUnitManager;

@Bean
@ConfigurationProperties("app.order.jpa")
public JpaProperties orderJpaProperties() {
return new JpaProperties();
}

@Bean
@ConfigurationProperties(prefix = "app.order.datasource")
public DataSource orderDataSource() {
Expand All @@ -29,7 +44,8 @@ public DataSource orderDataSource() {

@Bean
public LocalContainerEntityManagerFactoryBean orderEntityManager(
EntityManagerFactoryBuilder builder) {
JpaProperties orderJpaProperties) {
EntityManagerFactoryBuilder builder = createEntityManagerFactoryBuilder(orderJpaProperties);
return builder
.dataSource(orderDataSource())
.packages(Order.class)
Expand All @@ -42,4 +58,19 @@ public JpaTransactionManager orderTransactionManager(EntityManagerFactory orderE
return new JpaTransactionManager(orderEntityManager);
}

private EntityManagerFactoryBuilder createEntityManagerFactoryBuilder(JpaProperties customerJpaProperties) {
JpaVendorAdapter jpaVendorAdapter = createJpaVendorAdapter(customerJpaProperties);
return new EntityManagerFactoryBuilder(
jpaVendorAdapter, customerJpaProperties, this.persistenceUnitManager);
}

private JpaVendorAdapter createJpaVendorAdapter(JpaProperties jpaProperties) {
AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setShowSql(jpaProperties.isShowSql());
adapter.setDatabase(jpaProperties.getDatabase());
adapter.setDatabasePlatform(jpaProperties.getDatabasePlatform());
adapter.setGenerateDdl(jpaProperties.isGenerateDdl());
return adapter;
}

}
4 changes: 2 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
spring.jpa.properties.hibernate.hbm2ddl.auto=create

app.customer.datasource.url=jdbc:h2:mem:customers;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
app.customer.datasource.driver-class-name=org.h2.Driver
app.customer.jpa.properties.hibernate.hbm2ddl.auto=create

app.order.datasource.url=jdbc:h2:mem:orders;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
app.order.datasource.driver-class-name=org.h2.Driver
app.order.jpa.properties.hibernate.hbm2ddl.auto=create

0 comments on commit b585747

Please sign in to comment.