通过 XML 而不是注释注入 Entitymanager
我想做的是通过 XML 注入,几乎与通过 @PersistenceContext 注释完成的方式相同。我需要这个,因为我需要将不同的实体管理器注入到同一个 DAO 中。数据库相互镜像,我宁愿有 1 个基类,然后为该基类的实例创建多个类,这样我就可以使用 @PersistenceContext 注释。
这是我的例子。这就是我现在正在做的事情并且它有效。
public class ItemDaoImpl {
protected EntityManager entityManager;
public List<Item> getItems() {
Query query = entityManager.createQuery("select i from Item i");
List<Item> s = (List<Item>)query.getResultList();
return s;
}
public void setEntityManger(EntityManager entityManager) {
this.entityManager = entityManager;
}
}
@Repository(value = "itemDaoStore2")
public class ItemDaoImplStore2 extends ItemDaoImpl {
@PersistenceContext(unitName = "persistence_unit_2")
public void setEntityManger(EntityManager entityManager) {
this.entityManager = entityManager;
}
}
@Repository(value = "itemDaoStore1")
public class ItemDaoImplStore1 extends ItemDaoImpl {
@PersistenceContext(unitName = "persistence_unit_1")
public void setEntityManger(EntityManager entityManager) {
this.entityManager = entityManager;
}
}
TransactionManagers、EntityManagers 定义如下...
<!-- Registers Spring's standard post-processors for annotation-based configuration like @Repository -->
<context:annotation-config />
<!-- For @Transactional annotations -->
<tx:annotation-driven transaction-manager="transactionManager1" />
<tx:annotation-driven transaction-manager="transactionManager2" />
<!-- This makes Spring perform @PersistenceContext/@PersitenceUnit injection: -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<!-- Drives transactions using local JPA APIs -->
<bean id="transactionManager1" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory1" />
</bean>
<bean id="transactionManager2" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory2" />
</bean>
<bean id="entityManagerFactory1" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="persistence_unit_1"/>
...
</bean>
<bean id="entityManagerFactory2" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="persistence_unit_2"/>
...
</bean>
我想要做的是不创建类 ItemDaoImplStore2 或 ItemDaoImplStore1。我想通过 xml 将它们作为 ItemDaoImpl 的实例。但我不知道如何正确注入实体管理器。我想模拟将其注释为“存储库”注释,并且我还希望能够通过持久性单元名称指定要注入的实体管理器。我想要类似下面使用 XML 的内容。
<!-- Somehow annotate this instance as a @Repository annotation -->
<bean id="itemDaoStore1" class="ItemDaoImpl">
<!-- Does not work since it is a LocalContainerEntityManagerFactoryBean-->
<!-- Also I would perfer to do it the same way PersistenceContext works
and only provide the persistence unit name. I would like to be
able to specify persistence_unit_1-->
<property name="entityManager" ref="entityManagerFactory1"/>
</bean>
<!-- Somehow annotate this instance as a @Repository annotation -->
<bean id="itemDaoStore2" class="ItemDaoImpl">
<!-- Does not work since it is a LocalContainerEntityManagerFactoryBean-->
<!-- Also I would perfer to do it the same way PersistenceContext works
and only provide the persistence unit name. I would like to be
able to specify persistence_unit_2-->
<property name="entityManager" ref="entityManagerFactory2"/>
</bean>
What I am trying to do is inject through XML almost the same way that is done through A @PersistenceContext annotation. I am in need of this because of the fact I have different entity managers I need to inject into the same DAO. The databases mirror one another and I would rather have 1 base class and for instances of that base class then create multiple classes just so I can use the @PersistenceContext annotation.
Here is my example. This is what I am doing now and it works.
public class ItemDaoImpl {
protected EntityManager entityManager;
public List<Item> getItems() {
Query query = entityManager.createQuery("select i from Item i");
List<Item> s = (List<Item>)query.getResultList();
return s;
}
public void setEntityManger(EntityManager entityManager) {
this.entityManager = entityManager;
}
}
@Repository(value = "itemDaoStore2")
public class ItemDaoImplStore2 extends ItemDaoImpl {
@PersistenceContext(unitName = "persistence_unit_2")
public void setEntityManger(EntityManager entityManager) {
this.entityManager = entityManager;
}
}
@Repository(value = "itemDaoStore1")
public class ItemDaoImplStore1 extends ItemDaoImpl {
@PersistenceContext(unitName = "persistence_unit_1")
public void setEntityManger(EntityManager entityManager) {
this.entityManager = entityManager;
}
}
TransactionManagers, EntityManagers are defined below...
<!-- Registers Spring's standard post-processors for annotation-based configuration like @Repository -->
<context:annotation-config />
<!-- For @Transactional annotations -->
<tx:annotation-driven transaction-manager="transactionManager1" />
<tx:annotation-driven transaction-manager="transactionManager2" />
<!-- This makes Spring perform @PersistenceContext/@PersitenceUnit injection: -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<!-- Drives transactions using local JPA APIs -->
<bean id="transactionManager1" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory1" />
</bean>
<bean id="transactionManager2" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory2" />
</bean>
<bean id="entityManagerFactory1" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="persistence_unit_1"/>
...
</bean>
<bean id="entityManagerFactory2" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="persistence_unit_2"/>
...
</bean>
What I want to do is to NOT create classes ItemDaoImplStore2 or ItemDaoImplStore1. I want to have these as instances of ItemDaoImpl via xml instead. I do not know how to inject the entitymanager properly though. I want to simulate annotating this as a 'Repository' annotation, and I also want to be able to specify what entityManager to inject by the persistence unit name. I want something similar to the below using XML instead.
<!-- Somehow annotate this instance as a @Repository annotation -->
<bean id="itemDaoStore1" class="ItemDaoImpl">
<!-- Does not work since it is a LocalContainerEntityManagerFactoryBean-->
<!-- Also I would perfer to do it the same way PersistenceContext works
and only provide the persistence unit name. I would like to be
able to specify persistence_unit_1-->
<property name="entityManager" ref="entityManagerFactory1"/>
</bean>
<!-- Somehow annotate this instance as a @Repository annotation -->
<bean id="itemDaoStore2" class="ItemDaoImpl">
<!-- Does not work since it is a LocalContainerEntityManagerFactoryBean-->
<!-- Also I would perfer to do it the same way PersistenceContext works
and only provide the persistence unit name. I would like to be
able to specify persistence_unit_2-->
<property name="entityManager" ref="entityManagerFactory2"/>
</bean>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 SharedEntityManagerBean - 它为 EntityManagerFactory 创建共享的 EntityManager ,方式与 @PersistenceContext 相同:
Use
SharedEntityManagerBean
- it creates a sharedEntityManager
forEntityManagerFactory
the same way as@PersistenceContext
:您可以使用
SharedEntityManagerBean
在 xml 配置中提供持久性单元名称,如下所示:当然,您可以拥有
SharedEntityManagerBean
> 作为一个单独的 bean在这里,我将
entityManager
注入到BaseDAO
中,就像使用@PersistenceContext(unitName="..." )
You can provide the persistence unit name in the xml configuration, using the
SharedEntityManagerBean
, like below:of course, you can have the
SharedEntityManagerBean
as a separate beanHere, I m injecting
entityManager
intoBaseDAO
as you're doing using@PersistenceContext(unitName="...")