问题描述
我正在将JPA与Hibernate一起使用.当我修改一个对象的一个属性并更新它时;生成的SQL显示所有列正在更新!为什么不只更新已修改的列?有没有一种方法可以实现这一目标,因为我认为它将得到更好的优化.
I am using JPA with Hibernate. When I modify one attribute of an object and update it; the generated SQL shows all the columns getting updated ! Why doesn't it update only the modified columns? Is there a way to achieve that because I feel that will be more optimized.
推荐答案
默认情况下,hibernate包含更新查询中的所有字段.如果要排除此错误,请使用自定义更新HQL ,或者您可以将休眠配置为排除更新查询中未修改的字段,如此文章.
By default hibernate includes all the fields in the update query. If you want to exclude this either use a custom update HQL or you can configure hibernate to exclude the unmodified fields in the update query as told in this article.
这是通过在类映射中添加dynamic-update="true"
来完成的.
This is done by adding dynamic-update="true"
in your class mapping.
<class ... table="your_table" .... dynamic-update="true">
在具有许多列的大型表中(旧式设计)或包含大量数据,这将对系统性能产生重大影响.如此处所述,它可能会对性能产生影响.因此,在实施代码之前,请先评估其性能.
In a large table with many columns (legacy design) or contains large data volumes, this will have a great impact on the system performance. It can have some performance impact as told here. So measure the performance of your code before you implement it.
阅读API 这里以获取更多信息.
Read the API here for more information.
如果您使用的是注释,
@org.hibernate.annotations.Entity(selectBeforeUpdate=true)
这是特定于休眠的注释,在JPA中不可用.这是一个很好的文章链接到详细信息.
It is a hibernate specific annotation, and not available in JPA. Here is a good article linking to the details.
这篇关于为什么在JPA Hibernate中更新查询;所有属性都将在SQL中更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!