I have a class that is responsible for execute stored procedure, it was working fine when I was using JTA... But cause I have some problems with redeploy, I removed JTA and I'm using local entity manager with 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>
Now I get a NullPointerException when I try to get active session:
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();
}
.
.
.
}
ADDED
Probably the problem is that entityManager doesn't have a transaction. I'm trying create the transaction with spring aop, it works for all other classes, but doesn't works for interface 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>
Could someone explain me why I can't get the Session for me invoke stored procedure??
The spring way of doing this is to simply inject the EntityManager with the @PersistenceContext annotation and return the delegate...
public class ExecutadorProcedimentoArmazenado extends BaseDao implements IExecutadorProcedimentoArmazenado {
@PersistenceContext
private EntityManager entitymanager
private Session configurarSessao() {
return ((JpaEntityManager) entityManager.getDelegate()).getActiveSession();
}
If the active session is null, it means there is no active session. I tested this and outside of a transaction, it gives an NPE. Inside a transaction, the code above works. Tested with Spring 3.1 / Eclipselink 2.0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With