Java 在 Spring Boot 中获取 EntityManager 的句柄

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32020249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:55:59  来源:igfitidea点击:

Obtaining handle to EntityManager in Spring Boot

javaspring-bootjpaspring-data-jpaspring-data

提问by pastafarian

Is there any way to get a handle to the EntityManager for a given entity object? I'm using spring boot 1.2.3 with JPA starter and i'm further explicitly configuring multiple data sources with @configuration

有没有办法获得给定实体对象的 EntityManager 句柄?我正在使用带有 JPA starter 的 spring boot 1.2.3,并且我正在进一步明确地配置多个数据源@configuration

I've checked [resolved]SPRING BOOT access to entityManagerand it doesn't seem to answer the question.

我已经检查了[resolved] SPRING BOOT 对 entityManager 的访问,但它似乎没有回答这个问题。

Thanks.

谢谢。

EDIT: I added description of how my data sources are defined:

编辑:我添加了如何定义我的数据源的描述:

@Component
@Configuration
public class DataSources {
    @Bean
    @Primary
    @ConfigurationProperties(prefix="first.datasource")
    public DataSource getPrimaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix="second.datasource")
    public DataSource getSecondDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix="third.final.datasource")
    public DataSource getThirdFinalDataSource() {
        return DataSourceBuilder.create().build();
    }
}

In my application.yml I have the following sections

在我的 application.yml 我有以下部分

first.datasource:
  name: 'first_datasource',
  #other attributes...
second.datasource:
  name: 'second_datasource',
  #other attributes...
third.final.datasource:
  name: 'first_datasource',
  #other attributes...

So far I've tried both of @Stephane's suggestions but I get NoSuchBeanDefinitionException

到目前为止,我已经尝试了@Stephane 的两个建议,但我得到了 NoSuchBeanDefinitionException

Let's say my entity is called Customerthen I tried

假设我的实体被调用Customer然后我尝试

@Service
public class FooService {

    private final EntityManager entityManager;

    @Autowired
    public FooService(@Qualifier("customerEntityManager") EntityManager entityManager) {
        ...
    }

}

But I also tried

但我也试过

@PersistenceContext(unitName = "customer") // also tried "customers" and "first_datasource"
private EntityManager entityManager;

with no luck.

没有运气。

采纳答案by Stephane Nicoll

It depends how you've been configuring this but have you tried to injectthe EntityManagerwith a qualifier that corresponds to the factory that created it?

这取决于您如何配置它,但是您是否尝试使用与创建它的工厂相对应的限定符注入EntityManager

Here is a sample project with two data sources. If you want to inject the EntityManagerfor order, just do the following in any Spring bean of the project

这是一个带有两个数据源的示例项目。如果要注入EntityManagerfor命令,只需在项目的任何Spring bean中执行以下操作

@Service
public class FooService {

    private final EntityManager entityManager;

    @Autowired
    public FooService(@Qualifier("orderEntityManager") EntityManager entityManager) {
        ...
    }

}

For customer, use the customerEntityManager.

对于客户,请使用customerEntityManager.

Of course you can use the persistent unit name instead, that is

当然你也可以使用持久化的单元名来代替,即

@PersistenceContext(unitName = "customers")
private EntityManager entityManager;

@PersistenceContext(unitName = "orders")
private EntityManager entityManager;

Check the configuration of the project for more details.

检查项目的配置以获取更多详细信息。