Skip to content

Commit 020b08d

Browse files
committed
#289 - Simplified EntityManager setup in multiple DataSources example.
We now use Spring Boot's EntityManagerFactoryBuilder to simplify the setup. Removed the manual exclusion of the HibernateJpaAutoConfiguration as it's not needed anymore.
1 parent 013058d commit 020b08d

File tree

3 files changed

+39
-47
lines changed

3 files changed

+39
-47
lines changed

jpa/multiple-datasources/src/main/java/example/springdata/jpa/multipleds/Application.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.springframework.boot.autoconfigure.SpringBootApplication;
2525
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
2626
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
27-
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
2827
import org.springframework.boot.devtools.autoconfigure.DevToolsDataSourceAutoConfiguration;
2928
import org.springframework.transaction.annotation.EnableTransactionManagement;
3029

@@ -44,8 +43,8 @@
4443
* @see example.springdata.jpa.multipleds.customer.CustomerConfig
4544
* @see example.springdata.jpa.multipleds.order.OrderConfig
4645
*/
47-
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
48-
DataSourceTransactionManagerAutoConfiguration.class })
46+
@SpringBootApplication(
47+
exclude = { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class })
4948
@EnableTransactionManagement
5049
public class Application {
5150

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,56 +18,53 @@
1818
import javax.persistence.EntityManagerFactory;
1919
import javax.sql.DataSource;
2020

21+
import org.springframework.beans.factory.annotation.Qualifier;
22+
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
2123
import org.springframework.context.annotation.Bean;
2224
import org.springframework.context.annotation.Configuration;
2325
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
2426
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
2527
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
2628
import org.springframework.orm.jpa.JpaTransactionManager;
2729
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
28-
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
2930
import org.springframework.transaction.PlatformTransactionManager;
3031

3132
/**
3233
* Configuration for the {@link Customer} slice of the system. A dedicated {@link DataSource},
33-
* {@link JpaTransactionManager} and {@link EntityManagerFactory}. Note that there could of course be some deduplication
34-
* with {@link example.springdata.jpa.multipleds.order.OrderConfig}. I just decided to keep it to focus on the
35-
* sepeartion of the two. Also, some overlaps might not even occur in real world scenarios (whether to create DDl or the
36-
* like).
34+
* {@link JpaTransactionManager} and {@link EntityManagerFactory}. Note that there could of course be some
35+
* de-duplication with {@link example.springdata.jpa.multipleds.order.OrderConfig}. I just decided to keep it to focus
36+
* on the separation of the two. Also, some overlaps might not even occur in real world scenarios (whether to create DDl
37+
* or the like).
3738
*
3839
* @author Oliver Gierke
3940
*/
4041
@Configuration
41-
@EnableJpaRepositories(entityManagerFactoryRef = "customerEntityManagerFactory",
42+
@EnableJpaRepositories( //
43+
entityManagerFactoryRef = "customerEntityManagerFactory", //
4244
transactionManagerRef = "customerTransactionManager")
4345
class CustomerConfig {
4446

4547
@Bean
46-
PlatformTransactionManager customerTransactionManager() {
47-
return new JpaTransactionManager(customerEntityManagerFactory().getObject());
48+
PlatformTransactionManager customerTransactionManager(
49+
@Qualifier("customerEntityManagerFactory") EntityManagerFactory emf) {
50+
return new JpaTransactionManager(emf);
4851
}
4952

5053
@Bean
51-
LocalContainerEntityManagerFactoryBean customerEntityManagerFactory() {
54+
LocalContainerEntityManagerFactoryBean customerEntityManagerFactory(EntityManagerFactoryBuilder builder) {
5255

53-
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
54-
jpaVendorAdapter.setGenerateDdl(true);
55-
56-
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
57-
58-
factoryBean.setDataSource(customerDataSource());
59-
factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
60-
factoryBean.setPackagesToScan(CustomerConfig.class.getPackage().getName());
61-
62-
return factoryBean;
56+
return builder //
57+
.dataSource(customerDataSource()) //
58+
.packages(CustomerConfig.class) //
59+
.build();
6360
}
6461

6562
@Bean
6663
DataSource customerDataSource() {
6764

68-
return new EmbeddedDatabaseBuilder().//
69-
setType(EmbeddedDatabaseType.HSQL).//
70-
setName("customers").//
71-
build();
65+
return new EmbeddedDatabaseBuilder() //
66+
.setType(EmbeddedDatabaseType.HSQL) //
67+
.setName("customers") //
68+
.build();
7269
}
7370
}

jpa/multiple-datasources/src/main/java/example/springdata/jpa/multipleds/order/OrderConfig.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,48 +18,44 @@
1818
import javax.persistence.EntityManagerFactory;
1919
import javax.sql.DataSource;
2020

21+
import org.springframework.beans.factory.annotation.Qualifier;
22+
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
2123
import org.springframework.context.annotation.Bean;
2224
import org.springframework.context.annotation.Configuration;
2325
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
2426
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
2527
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
2628
import org.springframework.orm.jpa.JpaTransactionManager;
2729
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
28-
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
2930
import org.springframework.transaction.PlatformTransactionManager;
3031

3132
/**
3233
* Configuration for the {@link Order} slice of the system. A dedicated {@link DataSource},
33-
* {@link JpaTransactionManager} and {@link EntityManagerFactory}. Note that there could of course be some deduplication
34-
* with {@link example.springdata.jpa.multipleds.customer.CustomerConfig}. I just decided to keep it to focus on the
35-
* sepeartion of the two. Also, some overlaps might not even occur in real world scenarios (whether to create DDl or the
36-
* like).
34+
* {@link JpaTransactionManager} and {@link EntityManagerFactory}. Note that there could of course be some
35+
* de-duplication with {@link example.springdata.jpa.multipleds.customer.CustomerConfig}. I just decided to keep it to
36+
* focus on the separation of the two. Also, some overlaps might not even occur in real world scenarios (whether to
37+
* create DDl or the like).
3738
*
3839
* @author Oliver Gierke
3940
*/
4041
@Configuration
41-
@EnableJpaRepositories(entityManagerFactoryRef = "orderEntityManagerFactory",
42+
@EnableJpaRepositories(//
43+
entityManagerFactoryRef = "orderEntityManagerFactory", //
4244
transactionManagerRef = "orderTransactionManager")
4345
class OrderConfig {
4446

4547
@Bean
46-
PlatformTransactionManager orderTransactionManager() {
47-
return new JpaTransactionManager(orderEntityManagerFactory().getObject());
48+
PlatformTransactionManager orderTransactionManager(@Qualifier("orderEntityManagerFactory") EntityManagerFactory emf) {
49+
return new JpaTransactionManager(emf);
4850
}
4951

5052
@Bean
51-
LocalContainerEntityManagerFactoryBean orderEntityManagerFactory() {
53+
LocalContainerEntityManagerFactoryBean orderEntityManagerFactory(EntityManagerFactoryBuilder builder) {
5254

53-
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
54-
vendorAdapter.setGenerateDdl(true);
55-
56-
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
57-
58-
factoryBean.setDataSource(orderDataSource());
59-
factoryBean.setJpaVendorAdapter(vendorAdapter);
60-
factoryBean.setPackagesToScan(OrderConfig.class.getPackage().getName());
61-
62-
return factoryBean;
55+
return builder //
56+
.dataSource(orderDataSource()) //
57+
.packages(OrderConfig.class) //
58+
.build();
6359
}
6460

6561
@Bean

0 commit comments

Comments
 (0)