gpt4 book ai didi

java - 如何使用 entityManager 在 JPA 中启动事务

转载 作者:搜寻专家 更新时间:2023-10-31 20:00:12 26 4
gpt4 key购买 nike

我已经开始开发一个使用 spring、hibernate、JPA、SOAP web 服务的应用程序。现在要求某些查询必须在事务中运行。如果任何一个失败,整个事务应该回滚。

dao层中的代码如下:

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
import org.hibernate.Session;

public class BillDAOImpl implements BillDao{

@PersistenceContext(type = PersistenceContextType.EXTENDED)
private EntityManager em;

public boolean processBills() throws Exception{

EntityTransaction tx = null;
Session session = null;

try{

session = em.unwrap(Session.class);
tx = em.getTransaction();

Bill bill = em.find(Bill.class, billId);

//session.beginTransaction();
tx.begin();
...
...
em.persist(bill);
...
...
em.merge(<other object>);
...
...
//session.getTransaction().commit();
tx.commit();
} catch(){
}

}

}

当它执行 tx = em.getTransaction() 时,它给出以下错误:

java.lang.IllegalStateException: Cannot execute getTransaction() on a container-managed EntityManager

其他交易相关的属性如下:

<bean id="tuneEntityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:persistenceXmlLocation="classpath*:META-INF/tune-persistence.xml"
p:persistenceUnitName="tunePersistenceUnit" p:loadTimeWeaver-ref="loadTimeWeaver"
p:jpaVendorAdapter-ref="jpaVendorAdapter" p:jpaDialect-ref="jpaDialect"
p:dataSource-ref="tuneDbDataSource">
<property name="jpaProperties">
<props>
<prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.BTMTransactionManagerLookup
</prop>
<prop key="net.sf.ehcache.configurationResourceName">/${tune-db.ehcache.config.file}</prop>
<prop key="hibernate.transaction.flush_before_completion">false</prop>
<prop key="hibernate.default_schema">${tune-db.schema}</prop>
<prop key="org.hibernate.envers.default_schema">${tune-db.schema}</prop>
<prop key="javax.persistence.validation.mode">${tune-db.data.validation}</prop>
<prop key="hibernate.connection.isolation">3</prop>
<prop key="hibernate.connection.release_mode">auto</prop>
<prop key="hibernate.show_sql">${tune-db.hibernate.show-sql}</prop>
<prop key="hibernate.format_sql">${tune-db.hibernate.format-sql}</prop>
</props>
</property>
</bean>

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

当我使用 session.beginTransaction()session.getTransaction().commit() 时,它工作正常。

但是我想用 entityManager 的事务替换它。那应该怎么办呢?

最佳答案

尝试注入(inject) EntityManagerFactory,然后手动创建 EntityManager:

@PersistenceUnit
private EntityManagerFactory entityManagerFactory;

public boolean processBills() throws Exception{

EntityManager em = entityManagerFactory.createEntityManager();

EntityTransaction tx = null;

Session session = null;

try{

session = em.unwrap(Session.class);
tx = em.getTransaction();

关于java - 如何使用 entityManager 在 JPA 中启动事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42900265/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com