gpt4 book ai didi

java - Spring Data JPA CDI 与多个持久性单元集成

转载 作者:行者123 更新时间:2023-12-01 17:53:27 25 4
gpt4 key购买 nike

我是 Spring 新手,正在尝试在 Weblogic 12c 上集成 Spring Data、EclipseLink 和 EJB。

我想使用 CDI 将 Spring Data Repository 注入(inject)到无状态 EJB 中,因此我遵循 Spring Data CDI 集成指令并成功使用了单个持久性单元。

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpd.misc.cdi-integration

由于应用程序需要两个持久单元来连接两个不同的数据库,因此我在 persistence.xml 中配置了两个具有不同名称的持久单元。

问题来了:如何创建两个 Spring Data 存储库,以便 RepositoryA 使用持久化单元 A,RepositoryB 使用持久化单元 B?

持久性.xml

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="PRIMARY_PU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/EMP_DS</jta-data-source>
<class>com.smec.eis.example.springbooteval.model.Employee</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
</persistence-unit>
<persistence-unit name="SECONDARY_PU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/HR_DS</jta-data-source>
<class>com.smec.eis.example.springbooteval.model.Job</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
</persistence-unit>
</persistence>

主要 CDI 生产者:

public class EntityManagerFactoryProducer {

@Produces
@ApplicationScoped
public EntityManagerFactory createEntityManagerFactory() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("PRIMARY_PU");
return emf;
}

public void close(@Disposes EntityManagerFactory entityManagerFactory) {
entityManagerFactory.close();
}

@Produces
@Dependent
public EntityManager createEntityManager(EntityManagerFactory entityManagerFactory) {
return entityManagerFactory.createEntityManager();
}

public void close(@Disposes EntityManager entityManager) {
entityManager.close();
}
}

最佳答案

TL;博士;

使用限定符来声明哪个存储库应使用哪个EntityManager

说明

Spring Data JPA 存储库默认在单个 EntityManager 上实现。 CDI 扩展将任何限定符从存储库接口(interface)传播到其 EntityManager 选择。由于限定符实际上是空的(不包括 @Default@Any),因此该扩展使用上面代码中的单个 EntityManager

创建并添加自己的限定符注释将为您完成这项工作:

预选赛

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
@interface MyFirstDatabase {

}

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
@interface MySecondDatabase {

}

存储库接口(interface)

@MyFirstDatabase
public interface SomeRepository extends CrudRepository<MyEntity, Long> { … }

@MySecondDatabase
public interface SomeOtherRepository extends CrudRepository<OtherEntity, Long> { … }

客户端使用接口(interface)

public class MyComponent {

@Inject
@MyFirstDatabase
SomeRepository someRepo;

@Inject
@MySecondDatabase
SomeOtherRepository someOtherRepo;
}

您的EntityManagerFactoryProducer:

public class EntityManagerFactoryProducer {

@Produces
@ApplicationScoped
@MyFirstDatabase
public EntityManagerFactory createEntityManagerFactory() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("PRIMARY_PU");
return emf;
}

@Produces
@ApplicationScoped
@MySecondDatabase
public EntityManagerFactory createEntityManagerFactory() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("SECONDARY_PU");
return emf;
}

public void close(@Disposes EntityManagerFactory entityManagerFactory) {
entityManagerFactory.close();
}

@Produces
@Dependent
@MyFirstDatabase
public EntityManager createEntityManager(@MyFirstDatabase EntityManagerFactory entityManagerFactory) {
return entityManagerFactory.createEntityManager();
}

@Produces
@Dependent
@MySecondDatabase
public EntityManager createEntityManager(@MySecondDatabase EntityManagerFactory entityManagerFactory) {
return entityManagerFactory.createEntityManager();
}

public void close(@Disposes EntityManager entityManager) {
entityManager.close();
}
}

上面的代码假设您使用的实体类型在两个数据源中不相同。如果您需要使用相同的实体类型,那么您将创建一个基本存储库接口(interface),用 @NoRepositoryBean 和两个派生接口(interface)对其进行注释,类似于上面的代码。

关于java - Spring Data JPA CDI 与多个持久性单元集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47527084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com