11

我想要做的是通过 XML 注入几乎与通过 A @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 的实例。我不知道如何正确注入实体管理器。我想模拟将此注释为“存储库”注释,并且我还希望能够通过持久性单元名称指定要注入的 entityManager。我想要类似于下面使用 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>
4

2 回答 2

16

使用- 它以与以下相同的方式SharedEntityManagerBean创建共享:EntityManagerEntityManagerFactory@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>
于 2010-06-01T16:25:04.027 回答
4

您可以使用 xml 配置中的持久性单元SharedEntityManagerBean名称,如下所示:

<bean id="testDao" class="com.test.persistence.dao.BaseDAO">
    <property name="entityManager">
        <bean class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
            <property name="persistenceUnitName" value="persistence-test-unit" />
        </bean>
    </property>
</bean>

当然,您可以SharedEntityManagerBean将 bean 作为单独的 bean

在这里,我正在注入entityManagerBaseDAO正在使用的@PersistenceContext(unitName="...")

于 2013-04-19T09:43:49.710 回答