好吧,我的项目使用 EJB 和 JPA(使用 Hibernate),但我不得不切换到 Spring。在此之前一切都运行良好。EJB 用于注入 EntityManager、控制事务等。
好的,当我切换到 Spring 时,我遇到了很多问题,因为我是 Spring 新手。但是在一切运行之后,我遇到了问题:数据永远不会保存在数据库中。我配置了我的 Spring 来控制事务,我在 JSF 中使用了 spring bean,它具有执行艰苦工作的 spring 服务。该服务注入了一个 EntityManager 并使用@Transactional REQUIRED。该服务将 EntityManager 传递给调用 entityManager.persist(bean) 的 DAO。
选择似乎运行良好,JTA 事务似乎运行良好(我在日志中看到),但实体没有保存!
这是日志:
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter: doFilterInternal() (linha 136): Opening JPA EntityManager in OpenEntityManagerInViewFilter
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.beans.factory.support.DefaultListableBeanFactory: doGetBean() (linha 245): Returning cached instance of singleton bean 'transactionManager'
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.orm.hibernate3.HibernateTransactionManager: getTransaction() (linha 365): Creating new transaction with name [br.org.cni.pronatec.controller.service.MontanteServiceImpl.adicionarValor]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.orm.hibernate3.HibernateTransactionManager: doBegin() (linha 493): Opened new Session [org.hibernate.impl.SessionImpl@2b2fe2f0] for Hibernate transaction
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.orm.hibernate3.HibernateTransactionManager: doBegin() (linha 504): Preparing JDBC Connection of Hibernate Session [org.hibernate.impl.SessionImpl@2b2fe2f0]
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.orm.hibernate3.HibernateTransactionManager: doBegin() (linha 569): Exposing Hibernate transaction as JDBC transaction [com.sun.gjc.spi.jdbc40.ConnectionHolder40@3bcd4840]
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler: doJoinTransaction() (linha 383): Joined JTA transaction
INFO: Hibernate:
select
hibernate_sequence.nextval
from
dual
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.orm.hibernate3.HibernateTransactionManager: processCommit() (linha 752): Initiating transaction commit
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.orm.hibernate3.HibernateTransactionManager: doCommit() (linha 652): Committing Hibernate transaction on Session [org.hibernate.impl.SessionImpl@2b2fe2f0]
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.orm.hibernate3.HibernateTransactionManager: doCleanupAfterCompletion() (linha 734): Closing Hibernate Session [org.hibernate.impl.SessionImpl@2b2fe2f0] after transaction
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.orm.hibernate3.SessionFactoryUtils: closeSession() (linha 800): Closing Hibernate Session
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter: doFilterInternal() (linha 154): Closing JPA EntityManager in OpenEntityManagerInViewFilter
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.orm.jpa.EntityManagerFactoryUtils: closeEntityManager() (linha 343): Closing JPA EntityManager
在日志中,我看到它正在提交事务,但我没有看到插入查询(Hibernate 正在打印任何查询)。我还看到 Hibernate 查找以获取序列 ID 的下一个值。但在那之后,它永远不会真正插入。
这是弹簧上下文配置:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="PronatecPU" />
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" >
<property name="transactionManagerName" value="java:/TransactionManager" />
<property name="userTransactionName" value="UserTransaction" />
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<tx:annotation-driven transaction-manager="transactionManager" />
这是我的persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="PronatecPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/pronatec</jta-data-source>
<class>br.org.cni.pronatec.model.bean.AgendamentoBuscaSistec</class>
<class>br.org.cni.pronatec.model.bean.AgendamentoExportacaoZeus</class>
<class>br.org.cni.pronatec.model.bean.AgendamentoImportacaoZeus</class>
<class>br.org.cni.pronatec.model.bean.Aluno</class>
<class>br.org.cni.pronatec.model.bean.Curso</class>
<class>br.org.cni.pronatec.model.bean.DepartamentoRegional</class>
<class>br.org.cni.pronatec.model.bean.Dof</class>
<class>br.org.cni.pronatec.model.bean.Escola</class>
<class>br.org.cni.pronatec.model.bean.Inconsistencia</class>
<class>br.org.cni.pronatec.model.bean.Matricula</class>
<class>br.org.cni.pronatec.model.bean.Montante</class>
<class>br.org.cni.pronatec.model.bean.ParametrosVingentes</class>
<class>br.org.cni.pronatec.model.bean.TipoCurso</class>
<class>br.org.cni.pronatec.model.bean.Turma</class>
<class>br.org.cni.pronatec.model.bean.UnidadeFederativa</class>
<class>br.org.cni.pronatec.model.bean.ValorAssistenciaEstudantil</class>
<class>br.org.cni.pronatec.model.bean.ValorHora</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="current_session_context_class" value="thread"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.SunONETransactionManagerLookup"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
这是我在托管 bean 中注入的服务:
@Service
@Scope("prototype")
@Transactional(propagation= Propagation.REQUIRED)
public class MontanteServiceImpl {
// more code
@PersistenceContext(unitName="PronatecPU", type= PersistenceContextType.EXTENDED)
private EntityManager entityManager;
// more code
// The method that is called by another public method that do something before
private void salvarMontante(Montante montante) {
montante.setDataTransacao(new Date());
MontanteDao montanteDao = new MontanteDao(entityManager);
montanteDao.salvar(montante);
}
// more code
}
我的 MontanteDao 继承自基础 DAO,如下所示:
public class MontanteDao extends BaseDao<Montante> {
public MontanteDao(EntityManager entityManager) {
super(entityManager);
}
}
BaseDao 中调用的方法是这样的:
public void salvar(T bean) {
entityManager.persist(bean);
}
如您所见,它只是选择注入的 entityManager 并调用 persist() 方法。事务由 Spring 控制,就像打印在日志中一样,但是插入查询永远不会打印在日志中,也永远不会保存。
我很抱歉我的英语不好。提前感谢谁提供帮助。
编辑:调试,我看到在每个 entityManager.persist(T) 之后,在“插入”类型的 actionQueue 中加入了另一个动作。毕竟,它只是对动作进行排队,而从不执行它们。有没有人已经遇到过这种问题?