hibernate 动态更新

dynamic-update (optional - defaults to false): specifies that UPDATE SQL should be generated at runtime and can contain only those columns whose values have changed.

Take into account that dynamic updates can have some impact on performance. There's a little overhead involved in using it, and it might be counterproductive if you don't have a large (maybe legacy) table with lots of columns that are unlikely to be modified. See this related question Hibernate : dynamic-update dynamic-insert - Performance Effects.

Hibernate < 4.0

If you're using Hibernate Annotations, use the Hibernate specific @Entity annotation along with theJPA one:

@org.hibernate.annotations.Entity(dynamicUpdate = true)

If you're using XML mappings:

<class name="User" table="user" dynamic-update="true">

Hibernate >= 4.0

The Hibernate-specific @Entity annotation has been deprecated in Hibernate 4.0, and is scheduled to disappear in 4.1. The proper way to use dynamic updates is to annotate the entity with@DynamicUpdate now (thanks @Tiny for the heads up).


影响

Hibernate caches the actual INSERT/SELECT/UPDATE SQL strings for each entity and the obvious benefit is that it doesn't have to compute the SQL when you want to persist, find or update an entity.

However, when using dynamic-insert or 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.

My point of view is that dynamic insert and dynamic update can be interesting for tables with a fat blob column or tables with a huge number of columns. In other cases, I'm not convinced that dynamic insert or update always means performance boost (I do not use them by default). But as always, you should measure it.



参考链接:

1 http://stackoverflow.com/questions/15760934/jpa2-0-hibernate-why-jpa-fires-query-to-update-all-columns-value-even-some-stat

2 http://stackoverflow.com/questions/3404630/hibernate-dynamic-update-dynamic-insert-performance-effects

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值