【发布时间】:2017-03-29 07:11:41
【问题描述】:
我有以下代码
实体
@Entity
public class Employee {
@Id
@GeneratedValue
long id;
@Column(name="first_name")
String firstName;
@Column(name="last_name")
String lastName;
@Column(name="salary")
int salary;
@ManyToOne
@JoinColumn(name="address")
Address address;
..... setter & getter
}
回购
public interface EmpRepository extends JpaRepository<Employee, Long>{}
服务
@Service
public class EmpService {
@Autowired
private EmpRepository empRepo;
@Autowired @Qualifier("primaryEntityManagerFactory") EntityManager em;
public List<Employee> findAll(){
return empRepo.findAll();
}
}
控制器
@RestController
@RequestMapping(value = "/demo")
public class DemoController {
@Autowired
private EmpService empService;
@RequestMapping("/abcd")
public List<Employee> findAll(){
return empService.findAll();
}
}
数据源配置(主要)
@Configuration
@EnableJpaRepositories(basePackages="com.example",
entityManagerFactoryRef = "primaryEntityManagerFactory")
public class DataSourceConfiguration {
@Bean(name="primaryDataSource")
@Primary
@ConfigurationProperties(prefix = "datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@Primary
public JpaTransactionManager transactionManager() {
JpaTransactionManager tm = new JpaTransactionManager();
tm.setEntityManagerFactory(primaryEntityManagerFactory().getObject());
return tm;
}
@Bean(name="primaryEntityManagerFactory")
@Primary
LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory() {
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(primaryDataSource());
factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
factoryBean.setPackagesToScan(DataSourceConfiguration.class.getPackage().getName());
return factoryBean;
}
}
辅助数据源配置
@Configuration
@EnableJpaRepositories(basePackages="com.example",
entityManagerFactoryRef = "secondaryEntityManagerFactory")
public class DS2Configuration {
@Bean(name="secondaryDataSource")
@ConfigurationProperties(prefix = "datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public JpaTransactionManager transactionManager() {
JpaTransactionManager tm = new JpaTransactionManager();
tm.setEntityManagerFactory(secondaryEntityManagerFactory().getObject());
return tm;
}
@Bean(name="secondaryEntityManagerFactory")
LocalContainerEntityManagerFactoryBean secondaryEntityManagerFactory() {
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(secondaryDataSource());
factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
factoryBean.setPackagesToScan(DataSourceConfiguration.class.getPackage().getName());
return factoryBean;
}
}
application.properties
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.show_sql=true
spring.jooq.sql-dialect=MYSQL
logging.level.org.springframework.data=DEBUG
# Primary DataSource configuration
datasource.primary.url=jdbc:mysql://127.0.0.1:3306/jpa
datasource.primary.username=root
datasource.primary.password=root
# Secondary DataSource configuration
datasource.secondary.url=jdbc:mysql://127.0.0.1:3306/jpa2
datasource.secondary.username=root
datasource.secondary.password=root
# Disable Spring DataSource auto-initialization
spring.datasource.initialize=false
server.port=8081
当我从 EmpService 实际运行函数 findAll() 时,即使我将辅助 entitymanagefactory 指定为,它也始终使用主数据源
@Autowired
@Qualifier("secondaryEntityManagerFactory")
EntityManager em;
如何解决这个问题? 附言- 请不要为此分享博客链接。
【问题讨论】:
标签: jpa spring-boot spring-data-jpa