Spring @PersistenceContext如何在我的GenericDao中手动注入实体管理器

时间:2011-06-01 13:40:37

标签: spring entitymanager

你好所有人: 我阅读了关于这一点的春季参考文献。

我会选择在我的DAO中使用@PersistenceContext来注入一个共享事务实体管理器,但由于我在两个指向2个不同持久性单元的entityManagerFactories上使用GenericDaoJpaImpl模式,所以我无法使用它。

所以现在我的应用程序中有这个配置:

entityManagerFactoryies:

<bean id="entityManagerFactoryIban0" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath:META-INF/contratto-persistence-iban0.xml" />
    </bean>

    <bean id="entityManagerFactoryCont0" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath:META-INF/contratto-persistence-cont0.xml" />
    </bean>

    <bean abstract="true" id="abstractDaoJpaImplIban0" lazy-init="false">
        <property name="entityManagerFactory" ref="entityManagerFactoryIban0" />
    </bean>

    <bean abstract="true" id="abstractDaoJpaImplCont0" lazy-init="false">
        <property name="entityManagerFactory" ref="entityManagerFactoryCont0" />
    </bean>

然后我的每个DAO都是GenericDaoImpl的一个实例:

@Repository
public class GenericDaoJpaImpl<T, ID extends Serializable> implements GenericDao<T, ID> {
    private Class<T> entityClass;

    private EntityManagerFactory entityManagerFactory;

    public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
        this.entityManagerFactory = entityManagerFactory;
    }

    public GenericDaoJpaImpl() {
        super();
    }

    public GenericDaoJpaImpl(Class<T> entityClass) {
        super();
        this.entityClass = entityClass;
    }

    /**
     * @see it.alten.intesasanpaolo.contratto.dao.common.GenericDao#getItemByID(java.io.Serializable)
     */
    @Override
    public T getItemByID(ID id) {
        EntityManager em = entityManagerFactory.createEntityManager();
        return em.find(entityClass, id);

    }

我以这种方式通过spring构建我的dao:

<bean id="eventMessageDao" parent="abstractDaoJpaImplCont0" class="it.alten.intesasanpaolo.contratto.dao.common.GenericDaoJpaImpl">
        <constructor-arg>
            <value>it.alten.intesasanpaolo.contratto.domain.event.OnlineEventMessage</value>
        </constructor-arg>
    </bean>

现在我想修改Spring文档中描述的GenericDaoJpaImpl不要与entityManagerFactory关联,我必须每次从entityManager创建但直接创建entityManager。

我想在上下文中定义它,我可以将它注入正确的抽象dao,从每个dao扩展。

<bean abstract="true" id="abstractDaoJpaImplIban0" lazy-init="false">
        <property name="entityManagerFactory" ref="entityManagerFactoryIban0" />
    </bean>

我怎样才能做到这一点?

亲切的问候 马西莫

2 个答案:

答案 0 :(得分:4)

您可以使用SharedEntityManagerBeanEntityManager构建交易EntityManagerFactory

<bean id="entityManagerFactoryIban0" 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">            
    ... 
</bean> 

<bean id="entityManagerIban0"  
    class="org.springframework.orm.jpa.support.SharedEntityManagerBean">            
    <property name="entityManagerFactory" ref="entityManagerFactoryIban0" />
</bean> 

<bean abstract="true" id="abstractDaoJpaImplIban0" lazy-init="false">
    <property name="entityManager" ref="entityManagerIban0" />
</bean> 

答案 1 :(得分:0)

您可以使用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="...")