Java 什么使用刷新模式“自动”或“提交”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18149876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What to use Flush Mode 'Auto' or 'Commit'
提问by commit
As my title described, I am using hibernate Auto
flush mode mechanism in my application. So, when I change any data in a hibernate persistent object, it reflects automatically in the database. I don't want this. So I found a solution to use FlushMode Commit
instead.
正如我的标题所描述的,我Auto
在我的应用程序中使用了休眠刷新模式机制。因此,当我更改休眠持久对象中的任何数据时,它会自动反映在数据库中。我不要这个。所以我找到了一个使用 FlushMode 的解决方案Commit
。
So here is my actual question:
所以这是我的实际问题:
- Is it better to use
Commit
flush mode instead ofAuto
? and What is the meaning of this statement from the documentation?
The Session is sometimes flushed before query execution in order to ensure that queries never return stale state.
- 使用
Commit
刷新模式而不是更好Auto
吗?和 文档中的此声明是什么意思?
Session 有时会在查询执行之前被刷新,以确保查询永远不会返回陈旧状态。
http://docs.jboss.org/hibernate/orm/3.5/javadoc/org/hibernate/FlushMode.html
http://docs.jboss.org/hibernate/orm/3.5/javadoc/org/hibernate/FlushMode.html
采纳答案by Barett
Hibernate (and JPA) are designed to automatically detect and persist changes to persistent objects to the database. There is no "save" operation.
Hibernate(和 JPA)旨在自动检测对持久对象的更改并将其持久化到数据库中。没有“保存”操作。
If you don't want things saved, you should use detached objects. Either use a StatelessSession
to load them, or call detach after loading your objects. This removes them from the monitoring that will automatically save them.
如果你不想保存东西,你应该使用分离的对象。要么使用 aStatelessSession
加载它们,要么在加载对象后调用 detach。这会将它们从将自动保存它们的监控中移除。
Don't mess with the flush settings, it will just give you headaches later.
不要弄乱冲洗设置,它只会让您稍后头疼。
回答by vitalidze
is it better to use Commit flush mode instead of Auto
使用 Commit 刷新模式而不是 Auto 更好吗
When your application uses queries the FlushMode.COMMIT
will most likely perform better because it will not flush session before each query. I know that per javadoc it should flush session only when necessary but from my experience FlushMode.COMMIT performs even better in read-only sessions. Auto-flush doesn't mean that any change to the persistent object is immediately posted to the database.
当您的应用程序使用查询时,FlushMode.COMMIT
它很可能会表现得更好,因为它不会在每次查询之前刷新会话。我知道每个 javadoc 应该只在必要时刷新会话,但根据我的经验 FlushMode.COMMIT 在只读会话中表现更好。自动刷新并不意味着对持久对象的任何更改都会立即发布到数据库中。
what is meaning of below statement specified in document
The Session is sometimes flushed before query execution in order to ensure that queries never return stale state.
文件中指定的以下语句的含义是什么
Session 有时会在查询执行之前被刷新,以确保查询永远不会返回陈旧状态。
As I've written above when FlushMode.AUTO (default) is used it will flush session before every query (HQL, Criteria, SQL query) made to the database to make sure results will contain all entities added within current session.
正如我上面写的,当使用 FlushMode.AUTO(默认)时,它将在对数据库进行的每个查询(HQL、Criteria、SQL 查询)之前刷新会话,以确保结果将包含在当前会话中添加的所有实体。