通过XML,而不是annnotations注入的EntityManager [英] Injecting Entitymanager via XML and not annnotations

查看:116
本文介绍了通过XML,而不是annnotations注入的EntityManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所试图做的是通过XML注入几乎是一个通过@PersistenceContext注释做了同样的方式。我需要这个,因为事实上我有不同的实体管理器,我需要注入同样的DAO。数据库镜像彼此,我宁愿有1基类和基类的实例,然后创建多个类,这样我可以使用@PersistenceContext注解。

下面是我的榜样。这就是我现在所做的和它的作品。

 公共类ItemDaoImp​​l {
 保护的EntityManager EntityManager的; 公开名单<项目> getItems(){
     查询查询= entityManager.createQuery(从项目我选择我);
  清单<项目> S =(列表<项目>)query.getResultList();
  返回S;
 }
 公共无效setEntityManger(EntityManager的EntityManager的){
  this.entityManager = EntityManager的;
 }
}@Repository(值=itemDaoStore2)
公共类ItemDaoImp​​lStore2扩展ItemDaoImp​​l { @PersistenceContext(的unitName =persistence_unit_2)
 公共无效setEntityManger(EntityManager的EntityManager的){
  this.entityManager = EntityManager的;
 }
}@Repository(值=itemDaoStore1)
公共类ItemDaoImp​​lStore1扩展ItemDaoImp​​l { @PersistenceContext(的unitName =persistence_unit_1)
 公共无效setEntityManger(EntityManager的EntityManager的){
  this.entityManager = EntityManager的;
 }
}

事务管理,EntityManagers定义如下...

 <! - 寄存器Spring标准的后处理器对于像@Repository基于注解的配置 -  GT&;
<背景:注解配置/><! - 对于@Transactional注解 - >
< TX:注解驱动的事务管理器=transactionManager1/>
< TX:注解驱动的事务管理器=transactionManager2/>< - 这使得弹簧执行@ PersistenceContext / @ PersitenceUnit注: - >
< bean类=org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor/><! - 使用本地的API JPA交易的驱动器 - >
<豆的id =transactionManager1级=org.springframework.orm.jpa.JpaTransactionManager>
 <属性名=entityManagerFactory的REF =entityManagerFactory1/>
< /豆><豆的id =transactionManager2级=org.springframework.orm.jpa.JpaTransactionManager>
 <属性名=entityManagerFactory的REF =entityManagerFactory2/>
< /豆><豆的id =entityManagerFactory1级=org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
 <属性名=persistenceUnitName来VALUE =persistence_unit_1/>
...
< /豆><豆的id =entityManagerFactory2级=org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
 <属性名=persistenceUnitName来VALUE =persistence_unit_2/>
...
< /豆>

我想要做的就是不要创建类ItemDaoImp​​lStore2或ItemDaoImp​​lStore1。我想通过XML有这些作为ItemDaoImp​​l的实例来代替。我不知道该如何正确虽然注入了EntityManager。我想模拟注释这是一个'仓库'的注释,同时我也希望能够以指定的EntityManager由持久性单元名称注入。我想类似下面使用XML来代替的东西。

 <! - 不知何故,这个注释实例作为@Repository注解 - >
 <豆的id =itemDaoStore1级=ItemDaoImp​​l>
  <! - 不工作,因为它是一个LocalContainerEntityManagerFactoryBean - >
  < - !另外我想perfer做同样的方式PersistenceContext作品
   并且只提供持久性单元名称。我想成为
   能够指定persistence_unit_1 - >
  <属性名=EntityManager的REF =entityManagerFactory1/>
 < /豆>  <! - 不知何故,这个注释实例作为@Repository注解 - >
 <豆的id =itemDaoStore2级=ItemDaoImp​​l>
  <! - 不工作,因为它是一个LocalContainerEntityManagerFactoryBean - >
  < - !另外我想perfer做同样的方式PersistenceContext作品
   并且只提供持久性单元名称。我想成为
   能够指定persistence_unit_2 - >
  <属性名=EntityManager的REF =entityManagerFactory2/>
 < /豆>


解决方案

使用 SharedEntityManagerBean - 它创建一个共享的EntityManager 的EntityManagerFactory 相同的方式, @PersistenceContext

 <豆的id =itemDaoStore1级=ItemDaoImp​​l>
    <属性名=EntityManager的>
        < bean类=org.springframework.orm.jpa.support.SharedEntityManagerBean>
            <属性名=entityManagerFactory的REF =entityManagerFactory1/>
        < /豆>
    < /性>
< /豆>

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>

解决方案

Use SharedEntityManagerBean - it creates a shared EntityManager for EntityManagerFactory the same way as @PersistenceContext:

<bean id="itemDaoStore1" class="ItemDaoImpl"> 
    <property name="entityManager">
        <bean class = "org.springframework.orm.jpa.support.SharedEntityManagerBean">
            <property name = "entityManagerFactory" ref="entityManagerFactory1"/>  
        </bean>
    </property>
</bean>

这篇关于通过XML,而不是annnotations注入的EntityManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆