EntityManager.flush做了什么以及为什么需要使用它? [英] What does EntityManager.flush do and why do I need to use it?

查看:278
本文介绍了EntityManager.flush做了什么以及为什么需要使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EJB,我将对象保存到数据库。在我看到的一个例子中,一旦保存了这个数据(EntityManager.persist),就会调用EntityManager.flush();为什么我需要这样做?我保存的对象没有附加,以后不会在方法中使用。事实上,一旦保存,方法返回,我希望资源被释放。 (示例代码也会在删除调用中执行此操作。)

I have an EJB where I am saving an object to the database. In an example I have seen, once this data is saved (EntityManager.persist) there is a call to EntityManager.flush(); Why do I need to do this? The object I am saving is not attached and not used later in the method. In fact, once saved the method returns and I would expect the resources to be released. (The example code does this on a remove call as well.)

if (somecondition) {
    entityManager.persist(unAttachedEntity);
} else {
    attachedEntityObject.setId(unAttachedEntity.getId());
}
entityManager.flush();


推荐答案

调用 EntityManager。 flush(); 将强制数据立即在数据库中持久存在,因为 EntityManager.persist()不会(取决于EntityManager的工作方式)配置: FlushModeType (AUTO或COMMIT)默认设置为AUTO和a flush将自动完成,如果它被设置为COMMIT,那么当提交事务时,数据的持久性将被延迟。

A call to EntityManager.flush(); will force the data to be persist in the database immediately as EntityManager.persist() will not (depending on how the EntityManager is configured : FlushModeType (AUTO or COMMIT) by default it's set to AUTO and a flush will be done automatically by if it's set to COMMIT the persitence of the data to the underlying database will be delayed when the transaction is commited).

这篇关于EntityManager.flush做了什么以及为什么需要使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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