我要做的是通过XML注入几乎与通过A @PersistenceContext注释完成的方式相同。我需要这个,因为我需要将不同的实体管理器注入到同一个DAO中。数据库彼此镜像,我宁愿有一个基类,对于那个基类的实例,然后创建多个类,这样我就可以使用@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>
答案 0 :(得分:14)
使用SharedEntityManagerBean
- 它为EntityManager
创建共享EntityManagerFactory
,方式与@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>
答案 1 :(得分:4)
您可以使用SharedEntityManagerBean
在xml配置中提供持久性单元名称,如下所示:
<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
在这里,我正在使用entityManager
BaseDAO
注入@PersistenceContext(unitName="...")