本文介绍了JPA - 何时在持久化对象时使用getTransaction()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始在Google App Engine上使用JPA。在阅读一些例子时,我注意到了对象持久化方式的一些变化。在一个例子中,我已经看到类似这样的内容:

  entityManager.getTransaction()。begin (); 
entityManager.persist(object);
entityManager.getTransaction()。commit();

在其他情况下,我没有看到使用getTransaction()。我只是看到了entityManager.persist(object)。什么时候使用getTransaction()是合适的?

解决方案c $ c>然后你使用JTA事务。因此,您不必干涉 EntityManager 使用 entityManager.getTransaction()获取的事务(更准确地说 - 您不能) code>。 JTA启动并提交您的事务。



如果您使用应用程序管理的 EntityManager ,并且您不想成为在部分JTA事务中,你需要自己管理它们(它被称为资源本地实体管理器)。



通常,应用程序管理在$ SE $环境中使用> EntityManager EntityManager.getTransaction()一起工作。 p>编辑:您可能对。


I've recently starting working with JPA on the Google App Engine. In reading some examples, I've noticed a couple variations in the way objects are persisted. In one case, I've seen something like this:

entityManager.getTransaction().begin();
entityManager.persist(object);
entityManager.getTransaction().commit();

In other cases, I don't see the use of getTransaction(). I simply see entityManager.persist(object). When is it appropriate to use getTransaction()?

解决方案

If you use container managed EntityManager then you're using JTA transactions. Hence, you don't need to (more precisely - you can not) interfere with EntityManager's transactions fetched using entityManager.getTransaction(). The JTA starts and commits your transaction.

If you use application managed EntityManager and you don't want to be in part of JTA transaction, then you need to manage them for yourself (it's called a resource-local entity manager).

Most typically, application managed EntityManager which works with EntityManager.getTransaction() is used in Java SE environment.

EDIT: You might be interested in secion 7.5 Controlling Transactions from the JPA 2.0 specification.

这篇关于JPA - 何时在持久化对象时使用getTransaction()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 06:21