3

我有一个负责执行存储过程的类,当我使用 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> 

现在,当我尝试获取活动会话时,我得到了 NullPointerException:

public class ExecutadorProcedimentoArmazenado extends BaseDao implements IExecutadorProcedimentoArmazenado {

    public boolean executar(String nomeProcedimento) {
        DataReadQuery query = configurarQuery(nomeProcedimento);
        registro = executarProcedimento(query);
        int resultado = Integer.parseInt(recuperarValorDeSaida("RESULTADO"));
        mensagem = recuperarValorDeSaida("MSGERRO");
        return resultado == 0;
    }
    .
    .
    private Session configurarSessao() {
        JpaEntityManager jpaEntityManager = JpaHelper.getEntityManager(entityManager);
        return jpaEntityManager.getActiveSession();
    }
    .
    .
    .
}

添加

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

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

有人可以解释一下为什么我不能为我调用存储过程获取会话吗?

4

2 回答 2

5

这样做的弹簧方式是简单地注入EntityManager注释@PersistenceContext并返回委托......

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 测试。

于 2012-08-14T16:57:17.577 回答
0

在这种情况下,您的容器是 Spring,并且可能会从 entityManager.getDelegate() 返回 null,从而导致 jpaEntityManager.getActiveSession() 上出现 NPE。尝试启动事务,或者直接从未包装的 EclipseLink EntityManagerFactory 获取 EntityManager。

于 2012-08-14T15:12:41.000 回答