TransactionRequiredException 执行更新/删除查询

2024-01-07

我将 hibernate JPA 与 spring 和 mongodb 结合使用,并在 Glassfish-4.0 上运行我的应用程序。

我的服务等级是:

@Component
public class Test {
    @PersistenceContext
    EntityManager em;
    EntityManagerFactory emf;
         
    @Transactional
    public String persist(Details details) {
        details.getUsername();
        details.getPassword();

        Query query = em.createNativeQuery("db.details.find(username="+details.getUsername()+"&password="+details.getPassword());

        em.getTransaction().begin();
        em.persist(details);
        em.getTransaction().commit();
        em.flush();
        em.clear();
        em.close();
        query.executeUpdate();
        System.out.println("Sucessful!");
        return "persist";        
    }
}

我的 spring-context.xml 是:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.javapapers.spring.mvc" />
    <context:annotation-config />
    <mvc:annotation-driven />
    <tx:annotation-driven transaction-manager="txManager" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="ogmTest"/>
    </bean>
    <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    </bean>
</beans>

我收到“TransactionRequiredException 执行更新/删除查询”

如何解决此错误?


但是,在网上搜索类似问题后,我不确定这是否会对您的情况有所帮助(即它是否仍然存在)。

我正在从持久性 EntityManager 创建本机查询来执行更新。

Query query = entityManager.createNativeQuery(queryString);

我收到以下错误:

导致:javax.persistence.TransactionRequiredException:正在执行 更新/删除查询

许多解决方案建议将 @Transactional 添加到您的方法中。 只是这样做并没有改变错误。

一些解决方案建议向 EntityManager 询问EntityTransaction这样你就可以调用开始并做出承诺。 这会引发另一个错误:

导致:java.lang.IllegalStateException:不允许创建 共享 EntityManager 上的事务 - 使用 Spring 事务或 EJB 代替 CMT

然后我尝试了一种方法,大多数网站都说是使用应用程序管理的实体管理器而不是容器管理的(我相信 Spring 是),那就是joinTransaction().

Having @Transactional修饰方法然后调用joinTransaction()在调用之前在 EntityManager 对象上query.executeUpdate()我的本机查询更新有效。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

TransactionRequiredException 执行更新/删除查询 的相关文章

随机推荐