如何使用 Web 服务和 EclipseLink 作为 JPA 提供程序更新数据库?
我使用 EclipseLink 作为 JPA 提供程序完成了一个简单的 Web 服务。我是 JPA 的初学者,所以我想知道这是否是更新数据库的常用方法和最纯粹、最安全的方法:
EntityManager em = emf.createEntityManager();
if(!em.getTransaction().isActive()){
em.getTransaction().begin();
}
Query query = (Query)em.createNamedQuery("Person.updatePerson");
query.setParameter("personId", person.getPersonPK().getPersonId())
.setParameter("personName", name);
return query.executeUpdate();
为什么我不需要使用 em.getTransaction().commit (); 在我调用 em.getTransaction().begin()
之后? query.executeUpdate()
似乎更新数据库,而不是提交调用。为什么会这样呢?有什么推荐的?
I have done a simple web service using EclipseLink as JPA provider. I am a beginner when coming to JPA, so I am wondering if this is the common way and most pure and secure way to make updates to the database:
EntityManager em = emf.createEntityManager();
if(!em.getTransaction().isActive()){
em.getTransaction().begin();
}
Query query = (Query)em.createNamedQuery("Person.updatePerson");
query.setParameter("personId", person.getPersonPK().getPersonId())
.setParameter("personName", name);
return query.executeUpdate();
Why don't I need to use em.getTransaction().commit();
after I have called em.getTransaction().begin()
? query.executeUpdate()
seems to update the database, not the commit call. Why is that so? What is recommended?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要调用 commit() 来提交事务。执行查询不会提交。
您使用什么类型的交易? JTA 还是 RESOURCE_LOCAL?如果您使用 JTA,则必须使用 JTA 开始提交事务。
启用日志记录以获取更多详细信息。
一般来说,在 JPA 中更新通常是通过读取对象并使用其设置方法来完成的,而不是执行原始查询。
You need to call commit() to commit the transaction. Executing a query will not commit.
What type of transactions are you using? JTA or RESOURCE_LOCAL? If you are using JTA, then you must use JTA to begin a commit transactions.
Enable logging to get more details.
In general in JPA updates are normally done by reading the object and using its set methods, not executing raw queries.
有趣的问题,我用非jta数据源做了一些调查:
我的解释方法(如果我错了,请纠正我):
em.getTransaction().commit()< /代码>。
Interesting question, I did some investigation with a non-jta-datasource:
My explanation approach (please correct me, if I am wrong):
em.getTransaction().commit()
.