javax.persistence.RollbackException:事务标记为rollbackOnly [英] javax.persistence.RollbackException: Transaction marked as rollbackOnly

查看:108
本文介绍了javax.persistence.RollbackException:事务标记为rollbackOnly的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring MVC进行游戏.我有一种获取游戏并进行更新的方法,但是当它进行更新时会出现错误,这是代码:

I'm making a game with Spring MVC. I have a method for get the game and update but when it goes to update it gives an error, this is the code:

HomeController.class

HomeController.class

@Transactional
@RequestMapping(value = "/partida/{idPartida}", method = RequestMethod.GET)
public String getPartida(@PathVariable("idPartida") long idPartida,
        Model model) throws IOException {
    Partida p = ServicioAplicacionPartida.getPartida(entityManager,
            idPartida);

    if (p.getJson() == null) {
        p.inicializarPartida(entityManager);
        ServicioAplicacionPartida.update(entityManager, p);
    }

GameDAO.class

GameDAO.class

@Transactional
public static Partida update(EntityManager entityManager, Partida p) {
    try {               
        Query q = entityManager.createNativeQuery("update Partida p SET p.json=:json where p.id=:id");
        q.setParameter("json", p.getJson());
        q.setParameter("id", p.getId());
        q.executeUpdate();
        return entityManager.find(Partida.class, p.getId());
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

执行"q.executeUdate()"行时发生错误,它是:

The error occurs when the line "q.executeUdate()" is executed, here it is:

javax.persistence.PersistenceException:org.hibernate.exception.DataException:无法执行语句

javax.persistence.PersistenceException: org.hibernate.exception.DataException: could not execute statement

这是服务器错误:

Estado HTTP 500-请求处理失败;嵌套的异常是org.springframework.transaction.TransactionSystemException:无法提交JPA事务.嵌套的异常是javax.persistence.RollbackException:事务标记为rollbackOnly

Estado HTTP 500 - Request processing failed; nested exception is org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly

我该如何解决?

推荐答案

您已将控制器和DAO方法标注为@Transactional,这是不正确的,因为@Transactional可以继承给内部方法.通常,事务应从服务层开始.

You have annotated controller and DAO methods as @Transactional, it is not correct as @Transactional can be inherited to the inner methods. Usually transaction should start at service layer.

尝试将这些参数添加到@Transactional批注中,然后将其从Controller或DAO中删除.

Try adding these parameters to @Transactional annotation and remove it from either Controller or DAO.:

@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor=Exception.class)

这篇关于javax.persistence.RollbackException:事务标记为rollbackOnly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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