TransactionRequiredException 执行更新/删除查询 [英] TransactionRequiredException Executing an update/delete query

查看:24
本文介绍了TransactionRequiredException 执行更新/删除查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 hibernate JPA 与 spring 和 mongodb 一起使用,并且我正在 Glassfish-4.0 上运行我的应用程序.

Hi I am using hibernate JPA with spring and mongodb and i am running my application on 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 是:

And my spring-context.xml is :

<?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>

我对代码进行了一些更改,但没有任何效果.任何人都可以帮我解决这个问题.提前致谢.

I have applied some changes in the code but their is no effect. Can anyone please help me to sort out this. Thanks in advance.

推荐答案

我不确定这是否对您的情况有帮助(也就是说,如果它仍然存在),但是,在网上搜索类似问题之后.

I am not sure if this will help your situation (that is if it stills exists), however, after scouring the web for a similar issue.

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

I was creating a native query from a persistence EntityManager to perform an update.

Query query = entityManager.createNativeQuery(queryString);

我收到以下错误:

引起:javax.persistence.TransactionRequiredException: Executing更新/删除查询

caused by: javax.persistence.TransactionRequiredException: Executing an update/delete query

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

Many solutions suggest adding @Transactional to your method. Just doing this did not change the error.

一些解决方案建议向 EntityManager 请求 EntityTransaction,以便您可以调用 begin 并自己提交.这会引发另一个错误:

Some solutions suggest asking the EntityManager for a EntityTransaction so that you can call begin and commit yourself. This throws another error:

caused by: java.lang.IllegalStateException: 不允许创建共享 EntityManager 上的事务 - 使用 Spring 事务或 EJBCMT 代替

caused by: java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead

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

I then tried a method which most sites say is for use application managed entity managers and not container managed (which I believe Spring is) and that was joinTransaction().

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

Having @Transactional decorating the method and then calling joinTransaction() on EntityManager object just prior to calling query.executeUpdate() and my native query update worked.

我希望这可以帮助遇到此问题的其他人.

I hope this helps someone else experiencing this issue.

这篇关于TransactionRequiredException 执行更新/删除查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆