Hibernate – Dynamic-Update Attribute Example
2014-04-22 14:27
363 查看
http://www.mkyong.com/hibernate/hibernate-dynamic-update-attribute-example/
The dynamic-update attribute tells Hibernate whether to include unmodified properties in the SQL UPDATE statement.
The default value of dynamic-update is false, which means include unmodified properties in the Hibernate’s SQL update statement.
For example, get an object and try modify its value and update it.
Hibernate will generate the following update SQL statement.
Hibernate will update all the unmodified columns.
If set the dynamic-insert to true, which means exclude unmodified properties in the Hibernate’s SQL update statement.
For example, get an object and try modify its value and update it again.
Hibernate will generate different update SQL statement.
Hibernate will update the modified columns only.
Performance issue
In a large table with many columns (legacy design) or contains large data volumes, update some unmodified columns are absolutely unnecessary and great impact on the system performance.
You can configure “
or XML mapping file.
This little “dynamic-update” tweak will definitely increase your system performance, and highly recommended to do it.
What is dynamic-update
The dynamic-update attribute tells Hibernate whether to include unmodified properties in the SQL UPDATE statement.
Dynamic-update example
1. dynamic-update=false
The default value of dynamic-update is false, which means include unmodified properties in the Hibernate’s SQL update statement.For example, get an object and try modify its value and update it.
Query q = session.createQuery("from StockTransaction where tranId = :tranId "); q.setParameter("tranId", 11); StockTransaction stockTran = (StockTransaction)q.list().get(0); stockTran.setVolume(4000000L); session.update(stockTran);
Hibernate will generate the following update SQL statement.
Hibernate: UPDATE mkyong.stock_transaction SET DATE=?, PRICE_CHANGE=?, PRICE_CLOSE=?, PRICE_OPEN=?, STOCK_ID=?, VOLUME=? WHERE TRAN_ID=?
Hibernate will update all the unmodified columns.
2. dynamic-update=true
If set the dynamic-insert to true, which means exclude unmodified properties in the Hibernate’s SQL update statement.For example, get an object and try modify its value and update it again.
Query q = session.createQuery("from StockTransaction where tranId = :tranId "); q.setParameter("tranId", 11); StockTransaction stockTran = (StockTransaction)q.list().get(0); stockTran.setVolume(4000000L); session.update(stockTran);
Hibernate will generate different update SQL statement.
Hibernate: UPDATE mkyong.stock_transaction SET VOLUME=? WHERE TRAN_ID=?
Hibernate will update the modified columns only.
Performance issue
In a large table with many columns (legacy design) or contains large data volumes, update some unmodified columns are absolutely unnecessary and great impact on the system performance.
How to configure it
You can configure “dynamic-update” properties via annotation
or XML mapping file.
1. Annotation
@Entity @Table(name = "stock_transaction", catalog = "mkyong") @org.hibernate.annotations.Entity( dynamicUpdate = true ) public class StockTransaction implements java.io.Serializable {
2. XML mapping
<class ... table="stock_transaction" catalog="mkyong" dynamic-update="true"> <id name="tranId" type="java.lang.Integer"> <column name="TRAN_ID" /> <generator class="identity" /> </id>
Conclusion
This little “dynamic-update” tweak will definitely increase your system performance, and highly recommended to do it.
相关文章推荐
- windows下图形界面转为控制台的main函数入口
- 2014-第九周项目一----<<,>>的运算符重载
- 2014微软编程之美初赛第一场第二题 树
- Android创建快捷方式,Phonegap创建快捷方式
- 转 FileStream Read File
- 计算彩色的BGR图像的直方图
- 编程杂项
- 小盆友 为什么你总是做不好营销?
- 10个核心的Linux面试问题与答案
- MongoDb 结果漂亮点!
- [Android学习笔记]Bitmap,BitmapDrawable,BitmapFactory学习笔记
- filestream read方法 循环读取固定文件
- /sys 下的目录结构
- android学习笔记---fragment适配手机和平板
- Cassandra与HBase的大数据对决 谁是胜者?
- 第九周-项目一运算符重载
- msi
- Android中的ID能否重名
- 关于指针数组和数组指针的浅谈
- activemq报EOFExceptionjvm错误