Java 在Spring Boot中获取EntityManager的句柄

Java 在Spring Boot中获取EntityManager的句柄,java,spring-boot,jpa,spring-data-jpa,spring-data,Java,Spring Boot,Jpa,Spring Data Jpa,Spring Data,有没有办法获得给定实体对象的EntityManager句柄?我将spring boot 1.2.3与JPA starter一起使用,并使用@configuration 我查过了,它似乎没有回答这个问题 谢谢 编辑:我添加了关于如何定义数据源的说明: @Component @Configuration public class DataSources { @Bean @Primary @ConfigurationProperties(prefix="first.dataso

有没有办法获得给定实体对象的EntityManager句柄?我将spring boot 1.2.3与JPA starter一起使用,并使用
@configuration

我查过了,它似乎没有回答这个问题

谢谢

编辑:我添加了关于如何定义数据源的说明:

@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();
    }
}
在我的application.yml中,我有以下部分

first.datasource:
  name: 'first_datasource',
  #other attributes...
second.datasource:
  name: 'second_datasource',
  #other attributes...
third.final.datasource:
  name: 'first_datasource',
  #other attributes...
到目前为止,我已经尝试了@Stephane的两个建议,但我没有得到任何这样的定义异常

假设我的实体名为
Customer
,然后我尝试

@Service
public class FooService {

    private final EntityManager entityManager;

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

}
但我也试过了

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

运气不好。

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

这是。如果要为order注入
EntityManager
,只需在项目的任何Springbean中执行以下操作

@Service
public class FooService {

    private final EntityManager entityManager;

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

}
对于客户,请使用
customerEntityManager

当然,您可以使用持久化单元名称,即

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

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

查看项目的配置以了解更多详细信息。

如果不了解您是如何配置JPA数据库的,就有点难说了。通常情况下,您应该能够使用
@PersistenceContext
,这样就可以开始了。你能告诉我们你是如何配置数据库的吗?谢谢,我知道了。仅当您需要主数据源以外的数据源时,才需要提供unitName。省略单位名称可以得到最终为我工作的主要实体经理。谢谢@Stéphane。在SpringBoot的情况下,我没有定义persistence.xml,我如何找到持久化单元的名称?我尝试了多次猜测,但我没有得到这样的定义,我感到困惑。Spring Boot不支持多实体管理器的自动配置,您必须自己配置。如果您有多个数据源,其中一个是
@Primary
,那么您的问题到底是什么?启动将仅在实体管理器上创建,因此添加
@PersistenceContext
(或
@Autowired
)是所需的全部。我确信我缺少一些东西。无论如何,我在关于如何创建数据源的问题中添加了更多细节。我不知道这是否有帮助。谢谢。就像我已经告诉过你的,Spring Boot不支持多实体管理器的自动配置,你必须自己配置。因此,您的项目有3个数据源和一个实体管理器(在主数据源上)。如果您插入实体管理器(没有限定符,普通的
@PersistenceContext
,您将得到唯一存在的实体管理器。您没有从它开始有点奇怪)。如果你想要3个实体经理,请按照我提供的示例项目链接。嗨,Stéphane。谢谢你的样品,做得很好!但我有一个问题,为什么实体和Rspository类必须在同一个包中?如果我想将它们拆分为不同的包,该怎么办?