Spring数据jpa中save和saveAndFlush的区别 [英] Difference between save and saveAndFlush in Spring data jpa

查看:7787
本文介绍了Spring数据jpa中save和saveAndFlush的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过 JpaRepository 来测试一些CRUD操作来学习Spring数据。

我碰到过两个方法保存 saveAndFlush
我没有得到这两者之间的区别。在调用 save 时,我的更改也被保存到数据库中,因此 saveAndFlush 的用处。

解决方案

saveAndFlush 上,更改将立即在此命令中刷新到DB。使用 save ,这不一定是真实的,并且可能会保留在内存中,直到 flush 或 commit 命令被发出。但请注意,即使您在事务中刷新了更改并且未提交它们,但这些更改对于外部事务仍然不可见,直到提交这个事务。

在你的情况中,你可能使用某种事务机制,它为你发出 commit 命令如果一切正常的话。


I am trying to learn spring data JPA by testing some CRUD operation via JpaRepository.

I came across two methods save and saveAndFlush. I don't get the difference between these two. On calling save also my changes are getting saved into database so what is the use of saveAndFlush.

解决方案

On saveAndFlush, changes will be flushed to DB immediately in this command. With save, this is not necessarily true, and might stay just in memory, until flush or commit commands are issued.

But be aware, that even if you flush the changes in transaction and do not commit them, the changes still won't be visible to the outside transactions until the commit in this transaction.

In your case, you probably use some sort of transactions mechanism, which issues commit command for you if everything works out fine.

这篇关于Spring数据jpa中save和saveAndFlush的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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