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.

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.

Follow up

1. Hibernate – dynamic-insert attribute example

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Hessam
Guest
Hessam

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

Guest
Guest
Guest

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 ?

John
Guest
John

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?

Sumit Gupta
Guest
Sumit Gupta

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’

Ankur Raiyani
Guest
Ankur Raiyani

I am using Spring 3 Hibernate 4 and it says org.hibernate.annotations.Entity is deprecated :(
Any solution?

Ankur Raiyani
Guest
Ankur Raiyani

Got the solutions.

Please use
@DynamicUpdate(value=true)

gmail
Guest
gmail

Since you are very good with Hibernate can you have a look at this post and advice about what is the problem?

Dean
Guest
Dean
We are using Spring’s JPA entity manager, but would like to use: @org.hibernate.annotations.Entity( dynamicUpdate = true ) 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. 2013-03-18 13:29:41.106:WARN:oejw.WebAppContext:Failed startup of context o.m.j.p.JettyWebAppContext{/project-web,file:/C:/dev/<project>/src/main/webapp/},file:/C:/dev/<project>/src/main/webapp/ org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/jpa.xml]: Invocation of init method failed; nested exception is java.lang.IllegalAccessError: tried to access method org.hibernate.cfg.Configuration.<init>(Lorg/hibernate/cfg/SettingsFactory;)V from class org.hibernate.ejb.Ejb3Configuration Has anyone come across this issue and if so, how were you able to fix it? Our initial thought… Read more »
ashish
Guest
ashish

Could not find better explanation than this..thanx

Peter Ögren
Guest
Peter Ögren

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
{

mohanReddy.k
Guest
mohanReddy.k

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.

Berkay
Guest
Berkay

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.”

Alejandro
Guest
Alejandro

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 ?

trackback
Why do JPA-generated update queries include more fields than necessary? | PHP Developer Resource

[…] You can configure hibernate to exclude the unmodified fields in the update query as told in this article. […]

Tez
Guest
Tez

I wanted to say the same thing Akthar has mentioned. Thank you for the article anyhow.

Akhtar
Guest
Akhtar

This article is misleading. In section 2 you want to use dynamic-update. Please spare a second to proof read before posting.

trackback
Dynamic-update with JPA | DeveloperQuestion.com
Mike Hazelwood
Guest
Mike Hazelwood

Awesome article. Thanks!

trackback
Hibernate Tutorials | Tutorials

[…] dynamic-update attribute example Using dynamic-insert to avoid the include unmodified properties in the SQL UPDATE statement. […]