|
1 | 1 | /*
|
2 |
| - * Copyright 2015 the original author or authors. |
| 2 | + * Copyright 2015-2017 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
18 | 18 | import javax.persistence.EntityManagerFactory;
|
19 | 19 | import javax.sql.DataSource;
|
20 | 20 |
|
| 21 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 22 | +import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; |
21 | 23 | import org.springframework.context.annotation.Bean;
|
22 | 24 | import org.springframework.context.annotation.Configuration;
|
23 | 25 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
24 | 26 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
25 | 27 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
26 | 28 | import org.springframework.orm.jpa.JpaTransactionManager;
|
27 | 29 | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
28 |
| -import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; |
29 | 30 | import org.springframework.transaction.PlatformTransactionManager;
|
30 | 31 |
|
31 | 32 | /**
|
32 | 33 | * 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). |
37 | 38 | *
|
38 | 39 | * @author Oliver Gierke
|
39 | 40 | */
|
40 | 41 | @Configuration
|
41 |
| -@EnableJpaRepositories(entityManagerFactoryRef = "customerEntityManagerFactory", |
| 42 | +@EnableJpaRepositories( // |
| 43 | + entityManagerFactoryRef = "customerEntityManagerFactory", // |
42 | 44 | transactionManagerRef = "customerTransactionManager")
|
43 | 45 | class CustomerConfig {
|
44 | 46 |
|
45 | 47 | @Bean
|
46 |
| - PlatformTransactionManager customerTransactionManager() { |
47 |
| - return new JpaTransactionManager(customerEntityManagerFactory().getObject()); |
| 48 | + PlatformTransactionManager customerTransactionManager( |
| 49 | + @Qualifier("customerEntityManagerFactory") EntityManagerFactory emf) { |
| 50 | + return new JpaTransactionManager(emf); |
48 | 51 | }
|
49 | 52 |
|
50 | 53 | @Bean
|
51 |
| - LocalContainerEntityManagerFactoryBean customerEntityManagerFactory() { |
| 54 | + LocalContainerEntityManagerFactoryBean customerEntityManagerFactory(EntityManagerFactoryBuilder builder) { |
52 | 55 |
|
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(); |
63 | 60 | }
|
64 | 61 |
|
65 | 62 | @Bean
|
66 | 63 | DataSource customerDataSource() {
|
67 | 64 |
|
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(); |
72 | 69 | }
|
73 | 70 | }
|
0 commit comments