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

发布于 2024-11-11 15:41:30 字数 2855 浏览 1 评论 0原文

大家好: 我读了关于这一点的 spring 参考资料。

我会选择在 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相关联,每次我都必须从entityManagerFactory创建entityManager,但直接创建entityManager。

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

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

我怎样才能实现这个目标?

亲切的问候 马西莫

Hallo all:
I read the spring reference about this point.

I would choose to use the @PersistenceContext in my DAO to inject a shared transactional entity manager, but since I use the GenericDaoJpaImpl pattern over two entityManagerFactories that point to 2 different persistence units I cannot use it.

So right now in my application I have this configuration:

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>

Then each of my DAOs is an instance of the 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);

    }

I construct my dao via spring in this way:

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

Now I would like to modify the GenericDaoJpaImpl as described in the spring documentation not to be associated to the entityManagerFactory from which I have to create every time the entityManager but directly to the entityManager.

I would like to define it in the context in a way I can inject it to the correct abstract dao to be extended from every dao.

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

How can I achieve this?

kind regards
Massimo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明媚如初 2024-11-18 15:41:30

您可以使用 SharedEntityManagerBeanEntityManagerFactory 构造事务性 EntityManager

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

You can use SharedEntityManagerBean to construct a transactional EntityManager from the 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> 
拒绝两难 2024-11-18 15:41:30

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

You can provide the persistence unit name in the xml configuration, using the SharedEntityManagerBean, like below:

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

of course, you can have the SharedEntityManagerBean as a separate bean

Here, I m injecting entityManager into BaseDAO as you're doing using @PersistenceContext(unitName="...")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文