gpt4 book ai didi

xml - 通过 XML 而不是注解注入(inject) Entitymanager

转载 作者:IT老高 更新时间:2023-10-28 13:46:03 26 4
gpt4 key购买 nike

我要做的是通过 XML 注入(inject)几乎与通过 @PersistenceContext 注释完成的方式相同。我之所以需要这个,是因为我需要将不同的实体管理器注入(inject)到同一个 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 的实例。我不知道如何正确注入(inject)实体管理器。我想模拟将此注释为“存储库”注释,并且我还希望能够通过持久性单元名称指定要注入(inject)的 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>

最佳答案

使用 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>

关于xml - 通过 XML 而不是注解注入(inject) Entitymanager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2951400/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com