我怎么能提交一个播放!手动 JPA 事务? [英] How can I commit a Play! JPA transaction manually?

查看:21
本文介绍了我怎么能提交一个播放!手动 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.

这篇关于我怎么能提交一个播放!手动 JPA 事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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