- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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();
}
}
最佳答案
使用限定符来声明哪个存储库应使用哪个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 {
}
@MyFirstDatabase
public interface SomeRepository extends CrudRepository<MyEntity, Long> { … }
@MySecondDatabase
public interface SomeOtherRepository extends CrudRepository<OtherEntity, Long> { … }
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/
我想知道将内存中的树结构存储为用于持久性目的的目录树的实用性。在我的例子中,他的目标文件系统将是 ZFS,一旦创建了结构,它将很少被多个进程访问。 使用目录树作为数据树持久化机制的性能如何? 最佳答案
我已经创建了 docker private registry 并且能够从同一网络中的其他 raspi 推送和拉取(将 registry ip 添加到 insecure-registry 选项) doc
我正在尝试构建我的第一个“真正的”Haskell 应用程序,一个使用 Servant 的 API,我在其中使用 Persistent 作为数据库后端。但是我在尝试使用 Persistent 进行某种类
我已经在 stackoverflow 上看到了 ASP.NET MVC C# 中的持久性 cookie 示例。但我不明白为什么下面的代码不起作用。 首先我写入cookie: HttpCookie co
我是 Java Persistence API 的新手.我刚学会它,现在想在我的 Java 桌面应用程序中使用它。但我有以下问题: Q1。哪个 JPA 实现的大小最小(因为我希望我的应用程序的大小尽可
我正在尝试按照 android 蓝图指南将 MVP 模式实现到我的项目中。目前,我将用户(实体数据)保存在我的 Presenter 中,我真的不喜欢这样做,因为在每次配置更改时,presenter 都
我需要一个类似 map 的数据结构(在 C++ 中)来存储具有以下功能的对 (Key,T): 可以在当前结构中插入新元素(Key,T) 可以在当前结构中根据Key搜索元素 您可以制作当前版本结构的“快
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
我的配置 MBean 的持久性存在问题。我的配置: @ManagedResource(objectName = "pl.mobile
docker 中的 RabbitMQ 在删除没有卷的容器后丢失数据。 我的 Dockerfile: FROM rabbitmq:3-management ENV RABBITMQ_HIPE_COMPI
我正在尝试编写一个类型类,以简化使用持久性、aeson 和 scotty 编写 CRUD 后端 这是我的想法: runDB x = liftIO $ do info CRUD a where
当我尝试使用 持久化对象时遇到问题多线程 . 详情: 假设我有一个对象 PaymentOrder其中有一个列表 PaymentGroup (一对多关系)和 PaymentGroup包含 CreditT
我想使用纯功能数据结构和以下操作来实现环形缓冲区 通过索引进行高效随机访问 添加到前面 从背面移除 使用持久数据结构的原因是因为我有一个写入器线程和多个读取器线程,并且我想避免读取器阻塞写入器。 这可
persistence.xml 中关于 derby 客户端驱动程序的属性声明应该是什么?例如: 当我想从 Java 更新数据库时,我不断收到下面的异常。 实际上我需要客户端驱动程序而不是 Embed
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
我希望用户即使关闭浏览器也不必登录。我的 cookie 将在一个月后过期。 用户登录成功 $session = CGI::Session->new (undef, undef, {Directory
我正在与 JBOss 作斗争,并且遇到了一个似乎很难解决的问题,但事实证明我不能。当我尝试部署一个简单的 Java Web 应用程序时,遇到异常: org.hibernate.ejb.Hibernat
当使用具有持久性的 Workflow Foundation 时,我们想删除一些(或全部)工作流,这些工作流不再应该被持久化/运行。 我可以使用任何脚本/工具吗? 最佳答案 没有可用于执行此操作的标准
我正在使用 JPA,并且我正在为所有引用实体使用二级缓存。一切正常,我可以从二级缓存中获取实体,因为它们之前已经被选中。 现在,我有两个应用程序,它们都使用相同的数据库(因此它们都使用相同的表、值等)
所以我正在制作一个 spring-boot 应用程序,并且从外部库导入一些数据模型(通过 maven 导入)。我有一些模型,它有一个外部库类型的字段,我希望能够保留它。像这样的东西: package
我是一名优秀的程序员,十分优秀!