我该如何进行Play!手动进行JPA交易? [英] How can I commit a Play! JPA transaction manually?

查看:123
本文介绍了我该如何进行Play!手动进行JPA交易?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,播放! 在请求成功完成后提交交易. 在Play中手动提交交易的正确方法是什么?

Usually, Play! commits the transaction after a request completes successfully. What is the correct way to commit a transaction manually in Play?

void addPerson() {
  Person p = new Person("John", "Doe");
  p.save();

  // TODO - commit the transaction

  // Now p should have an ID
  assert p.id != null;
  usePersonIdForSomethingNasty(p.id);
}

推荐答案

您可以通过调用JPA.em()获得Hibernate EntityManager.然后,您可以从那里访问事务(JPA.em().getTransaction()).

You can get the Hibernate EntityManager by calling JPA.em(). Then, from there, you have access to the transaction (JPA.em().getTransaction()).

如果您打算自己管理事务,则将要禁用Play!的事务处理(可以在方法或控制器上使用@NoTransaction注释来执行此操作).否则,播放!无论如何都会尝试在请求结束时提交事务,如果您自己已经完成了该操作,则会导致异常.

If you intend to manage the transaction yourself, you will want to disable Play!'s transaction handling (there is a @NoTransaction annotation you can use on the method or controller to do that). Otherwise, Play! will try to commit the transaction at the end of the request anyway, and if you have already done that yourself, that will cause an exception.

这篇关于我该如何进行Play!手动进行JPA交易?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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