EntityManager.flush 有什么作用,为什么我需要使用它? [英] What does EntityManager.flush do and why do I need to use it?

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

问题描述

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

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 并且刷新将自动完成,如果它设置为 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天全站免登陆