Hibernate – dynamic-update attribute example
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.
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.
Follow up
1. Hibernate – dynamic-insert attribute example
Unbelievable, this should be the default behavior.
Hi guys, i use session.load() to manipulate the real row. I think it is better to use load to get object proxy and working with it to update some fields of a row.
Thanks
for performance issue, it depends.
Reference documentation mentions that this could have negative performance effects.
“Although these settings can increase performance in some cases, they can actually decrease performance in others.”
HI mkyong,
Just wanted to add one thing : @DynamicUpdate(value=true) is applicable only for persistent objects i.e for non-persistent objects Hibernate’s SQL update statement will include unmodified properties also.
how do i update a column that is having null value by default using
@DynamicUpdate ?. when i changed null to empty it is working for me..
any idea how to do it ?
So how does Hibernate determine which columns have been modified? Does it issue a select statement based on the record being updated and then update accordingly, or does it keep track of the actual variables changed within the instance class?
hi is it possible to have multiple where clause in a hibernate query?
update MClass set classCode= ‘susu’,classDesc= ‘OneOne’ where classCode= ‘III’ and classDesc= ‘Third’
I am using Spring 3 Hibernate 4 and it says org.hibernate.annotations.Entity is deprecated 🙁
Any solution?
Got the solutions.
Please use
@DynamicUpdate(value=true)
Since you are very good with Hibernate can you have a look at this post and advice about what is the problem?
We are using Spring’s JPA entity manager, but would like to use:
within our Entity to be able to allow dynamic updates. We are getting errors which make it look like the hibernate-annotations.jar file is overriding something within our Spring jar.
Has anyone come across this issue and if so, how were you able to fix it? Our initial thought was that we’d have to specifically use a Hibernate Entity Manager, but according to this article that is unnecessary.
Any help is greatly appreciated.
Could not find better explanation than this..thanx
Great article! Off course you know that with Hibernate4 you can do
@Table(name = “foo”, schema = “public”, catalog = “fum”)
@Entity
@DynamicUpdate
public class Foo implements Serializable
{
Hi sir Please check the second para “2. Dynamic-Update=True” , in next line Dynamic-Insert=True , is there . I hope there correction is required.
What happens if i have to set null into a field on update ? modify to null. It changes it to null ? or omit the field because its null ?
I wanted to say the same thing Akthar has mentioned. Thank you for the article anyhow.
Hi Tez and Akhtar,
Sorry for the typo error, article is updated. Thanks and appreciated your proof read.
Awesome article. Thanks!
This article is misleading. In section 2 you want to use dynamic-update. Please spare a second to proof read before posting.