|
| 1 | +package com.baeldung.springmultipledatasources.topics; |
| 2 | + |
| 3 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 4 | +import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; |
| 5 | +import org.springframework.context.annotation.Bean; |
| 6 | +import org.springframework.context.annotation.Configuration; |
| 7 | +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
| 8 | +import org.springframework.orm.jpa.JpaTransactionManager; |
| 9 | +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; |
| 10 | +import org.springframework.transaction.PlatformTransactionManager; |
| 11 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 12 | + |
| 13 | +import javax.sql.DataSource; |
| 14 | +import java.util.Objects; |
| 15 | + |
| 16 | +@Configuration |
| 17 | +@EnableTransactionManagement |
| 18 | +@EnableJpaRepositories( |
| 19 | + basePackageClasses = Topic.class, |
| 20 | + entityManagerFactoryRef = "topicsEntityManagerFactory", |
| 21 | + transactionManagerRef = "topicsTransactionManager" |
| 22 | +) |
| 23 | +public class TopicJpaConfiguration { |
| 24 | + |
| 25 | + @Bean |
| 26 | + public LocalContainerEntityManagerFactoryBean topicsEntityManagerFactory( |
| 27 | + @Qualifier("topicsDataSource") DataSource dataSource, |
| 28 | + EntityManagerFactoryBuilder builder |
| 29 | + ) { |
| 30 | + return builder |
| 31 | + .dataSource(dataSource) |
| 32 | + .packages(Topic.class) |
| 33 | + .build(); |
| 34 | + } |
| 35 | + |
| 36 | + @Bean |
| 37 | + public PlatformTransactionManager topicsTransactionManager( |
| 38 | + @Qualifier("topicsEntityManagerFactory") LocalContainerEntityManagerFactoryBean topicsEntityManagerFactory) { |
| 39 | + return new JpaTransactionManager(Objects.requireNonNull(topicsEntityManagerFactory.getObject())); |
| 40 | + } |
| 41 | +} |
0 commit comments