Spring Can';不要获得日食的活跃时段';s带弹簧的整体管理器

Spring Can';不要获得日食的活跃时段';s带弹簧的整体管理器,spring,persistence,jpa-2.0,eclipselink,aop,Spring,Persistence,Jpa 2.0,Eclipselink,Aop,我有一个类负责执行存储过程,当我使用JTA时,它工作得很好。。。但由于我在重新部署方面遇到了一些问题,我删除了JTA,并将本地实体管理器用于spring: <bean id="entityManagerFactoryErp" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceUnitName" value="erp

我有一个类负责执行存储过程,当我使用JTA时,它工作得很好。。。但由于我在重新部署方面遇到了一些问题,我删除了JTA,并将本地实体管理器用于spring:

 <bean id="entityManagerFactoryErp" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="erpPU"/>
</bean>
<bean id="entityManagerErp" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
    <property name="entityManagerFactory" ref="entityManagerFactoryErp"/>
</bean> 
增加

可能问题在于entityManager没有事务。我正在尝试使用spring aop创建事务,它适用于所有其他类,但不适用于接口IExecutadorProceditionOArmazenado:

<bean id="entityManagerFactoryErp" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="erpPU"/>
</bean>

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

<bean id="transactionManagerErp"
      class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactoryErp"/>
</bean>

<tx:advice id="txExecutadorProcedimento" transaction-manager="transactionManagerErp">
    <tx:attributes>
        <tx:method name="executar" rollback-for="Exception" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:pointcut id="operacoesExecutadorProcedimento" expression="execution(* com.hrgi.persistencia.IExecutadorProcedimentoArmazenado.executar(..))"/>
    <aop:advisor advice-ref="txExecutadorProcedimento" pointcut-ref="operacoesExecutadorProcedimento"/>
</aop:config>


有人能解释一下为什么我无法获取会话for me invoke存储过程吗?

在本例中,您的容器是Spring,并且可能从entityManager.getDelegate()返回null,从而导致JPAEEntityManager.getActiveSession()上出现NPE。尝试启动事务,或者直接从未包装的EclipseLink EntityManager工厂获取EntityManager。

spring的方法是简单地使用
@PersistenceContext
注释注入
EntityManager
,然后返回委托

public class ExecutadorProcedimentoArmazenado extends BaseDao implements IExecutadorProcedimentoArmazenado {

  @PersistenceContext
  private EntityManager entitymanager

  private Session configurarSessao() {
    return ((JpaEntityManager) entityManager.getDelegate()).getActiveSession();
  }

如果活动会话为空,则表示没有活动会话。我测试了这个,在一个事务之外,它给出了一个NPE。在事务内部,上面的代码起作用。使用Spring 3.1/Eclipselink 2.0进行测试。

尝试使用entityManager.unwrap(EntityManagerImpl.class)展开;但是得到:java.lang.IllegalStateException:No transactional EntityManager Available对不起,我建议您直接从EntityManager工厂获取/展开EntityManager,并从中获取EclipseLink EntityManager,而不是从Spring中展开EntityManager。或者尝试在Spring中启动一个事务,然后打开EntityManager。不起作用。。。java.lang.ClassCastException:org.eclipse.persistence.internal.jpa.EntityManagerImpl无法转换为org.eclipse.persistence.sessions.Session。我已将转换更新为JPAEEntityManager(我不使用eclipselink)。既然您使用的是spring,您是否也考虑过使用SimpleJDBCall之类的东西?您的AOP配置看起来是正确的,但是为什么您仍然从eclipselink的JPAHelper获得EntityManager呢。如果你这样理解的话,我不确定Spring是否在管理EntityManager。您可以从我的代码示例中尝试基于注释的方法吗?我强烈建议使用@Transactional注释,而不是您在代码示例中使用的AOP方法。同样,我能够使用带有事务性和PersistenceContext注释的代码示例获得活动会话,这是当前Spring的最佳实践。
public class ExecutadorProcedimentoArmazenado extends BaseDao implements IExecutadorProcedimentoArmazenado {

  @PersistenceContext
  private EntityManager entitymanager

  private Session configurarSessao() {
    return ((JpaEntityManager) entityManager.getDelegate()).getActiveSession();
  }