dynamic-update = true如何在休眠状态下在内部工作? [英] How dynamic-update=true works internally in hibernate?

查看:69
本文介绍了dynamic-update = true如何在休眠状态下在内部工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我了解设置此属性 dynamic-update = true 的作用,即,它仅考虑那些已修改的字段,而忽略了为其他字段设置空值的工作. 节省开销,性能出色.

So I understand what setting this attribute dynamic-update=true does, i.e it considers only those fields that were modified and omits the job of setting null values for other fields. Saving an overhead, good on performance.

出于好奇:冬眠如何知道所有字段都被修改了?在触发更新查询之前,它是否先将选择查询生成的结果与数据库进行比较?假设是,那么比较不是性能的开销吗?

Asking out of curiosity: How does hibernate come to know what all fields were modified? Does it do a comparison of the result generated by the select query to the database first before firing an update query? Assuming yes, then isn't comparison an overhead to performance?

如果我错了,请纠正我.预先感谢!

Correct me if I'm wrong. Thanks in advance!

推荐答案

因此,在等待了将近2个月之后,我终于能够在各种来源的帮助下得出提出的结论,作为对我自己问题的回答:

So after almost 2 month's wait I was finally able to derive this conclusion proposed as an answer to my own question with the help from various sources:

1.)使用dynamic-update时,Hibernate必须每次生成相应的SQL字符串,因此Hibernate会降低性能 边.换句话说,在数据库端和Hibernate端的开销之间需要权衡.

1.) When using dynamic-update, Hibernate has to generate the corresponding SQL string each time and there is thus a performance cost on the Hibernate side. In other words, there is a trade-off between overhead on the database side and on the Hibernate side.

2.)Hibernate缓存每个实体的实际SELECT,INSERT和UPDATE SQL字符串.这样一来,您不必每次都要重新创建这些语句 查找,保留或更新实体.但是,使用dynamic-update时,Hibernate必须每次生成相应的SQL字符串. 这会导致Hibernate方面的性能损失.

2.) Hibernate caches the actual SELECT, INSERT and UPDATE SQL strings for each entity. This results in not having to re-create these statements every time you want to find, persist or update an entity.However, when using dynamic-update, Hibernate has to generate the corresponding SQL strings each time. This results in a performance cost on the Hibernate side.

3.)应用于非常小的&简单表,因为使用此注释可以显着提高性能. 在更现实的情况下,在使用远程数据库的更大表上,性能提升可能会更加明显.当然,您从中获得的里程将 变化大多数列都需要更新.

3.) Useful when applied to a very small & simple table as there is a significant performance gain using this annotation. A more realistic scenario, on wider tables using a remote database, performance increases may be more pronounced. Of course, the mileage you get out of this will vary most of the columns need to be updated.

4.)@DynamicUpdate annotation/ dynamic-update=true用于指定每当修改实体时都应生成UPDATE SQL语句. 默认情况下,Hibernate使用缓存的UPDATE语句来设置所有表列.当使用@DynamicUpdate批注对实体进行批注时, PreparedStatement将仅包含其值已更改的列.

4.) The @DynamicUpdate annotation/ dynamic-update=true is used to specify that the UPDATE SQL statement should be generated whenever an entity is modified. By default, Hibernate uses a cached UPDATE statement that sets all table columns. When the entity is annotated with the @DynamicUpdate annotation, the PreparedStatement is going to include only the columns whose values have been changed.

总体可以概括为:

  1. 运行时SQL生成开销.
  2. 不再使用PreparedStatement(缓存).
  3. 性能开销.

信用: Hibernate ORM 5.2.7.Final User Guide, http://memorynotfound.com/hibernate-dynamic-update-attriburte-example/ https://stackoverflow.com/a/3405560/1004631

Credits: Hibernate ORM 5.2.7.Final User Guide,http://memorynotfound.com/hibernate-dynamic-update-attriburte-example/ , https://stackoverflow.com/a/3405560/1004631

P.S .:对于dynamic-insert=true/ @DynamicInsert注释也适用

P.S.: Also holds true for dynamic-insert=true/ @DynamicInsert annotation

这篇关于dynamic-update = true如何在休眠状态下在内部工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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