EntityManager.flush()不刷新(JPA2(OpenJPA)、EJB3、Spring3、WebSphere7)
在我的项目中,我面临一个问题:entityManager.flush()没有做任何事情,而刷新只在提交之前,即退出EJB时进行 我的项目运行在WebSphere7上。 我通过OpenJPA使用JPA2。 我用弹簧自动接线。 我正在使用容器管理的事务 下面是相关的代码片段 persistence.xmlEntityManager.flush()不刷新(JPA2(OpenJPA)、EJB3、Spring3、WebSphere7),spring,jpa-2.0,ejb-3.0,openjpa,websphere-7,Spring,Jpa 2.0,Ejb 3.0,Openjpa,Websphere 7,在我的项目中,我面临一个问题:entityManager.flush()没有做任何事情,而刷新只在提交之前,即退出EJB时进行 我的项目运行在WebSphere7上。 我通过OpenJPA使用JPA2。 我用弹簧自动接线。 我正在使用容器管理的事务 下面是相关的代码片段 persistence.xml <persistence-unit name="persistenceUnit" transaction-type="JTA"> <provider>org.apache.
<persistence-unit name="persistenceUnit" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<properties>
<property name="openjpa.TransactionMode" value="managed" />
<property name="openjpa.ConnectionFactoryMode" value="managed" />
<property name="openjpa.DynamicEnhancementAgent" value="true" />
<property name="openjpa.jdbc.DBDictionary" value="StoreCharsAsNumbers=false" />
<property name="openjpa.Log" value="SQL=TRACE"/>
</properties>
</persistence-unit>
道:
“INSERT”仅在离开EJB时执行,这意味着DAO中的entityManager.flush()不工作
似乎问题在于实体管理器没有从WebSphere获取事务(可能是因为实体管理器是由Spring注入的,我还没有深入调查过)
因此,我所做的是让Spring控制EntityManager中的事务:
1. added <tx:annotation-driven/> and <tx:jta-transaction-manager/> to applicationContext.xml
2. annotated the DAO methods with @Transactional
1。向applicationContext.xml添加和
2.用@Transactional注释DAO方法
整个事务仍然由EJB处理,这意味着它仍然使用来自WebSphere的CMT和JTA
由于依赖地狱(最让我头疼的是hibernate内核,包括JBoss的javax.transaction实现,grr),我遇到了很多问题,但除此之外,一切似乎都进展顺利在执行“return jpaBaseEntityDao.persist(jpaBaseEntity)”之后,你说“离开EJB”是什么意思从JPABaseEntityServiceBean(就在返回控制器之前)中,当您说“插入仅在离开EJB时才完成”时,您的确切意思是什么?插入应该在flush()时执行,但在提交EJB的事务之前,数据在数据库中(或对其他事务)不可见;我是说,当我刷新()时,插入根本没有被执行
@Stateless(name="JPABaseEntityServiceBean")
@Configurable
public class JPABaseEntityServiceBean implements JPABaseEntityService {
Logger logger = LoggerFactory.getLogger(JPABaseEntityServiceBean.class);
@Autowired
JPABaseEntityDao jpaBaseEntityDao;
public JPABaseEntity persist(JPABaseEntity jpaBaseEntity) {
return jpaBaseEntityDao.persist(jpaBaseEntity);
}
@Repository
public class JPABaseEntityDao implements BaseEntityDao {
@PersistenceContext
transient EntityManager entityManager;
public JPABaseEntity persist(JPABaseEntity jpaBaseEntity) {
Date now = new Date();
jpaBaseEntity.setCreatedBy(TO_DO_ME);
jpaBaseEntity.setUpdatedBy(TO_DO_ME);
jpaBaseEntity.setUpdatedOn(now);
jpaBaseEntity.setCreatedOn(now);
entityManager.persist(jpaBaseEntity);
entityManager.flush();
return jpaBaseEntity;
}
1. added <tx:annotation-driven/> and <tx:jta-transaction-manager/> to applicationContext.xml
2. annotated the DAO methods with @Transactional