Main Tutorials

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 Author

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

Comments

Subscribe
Notify of
19 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Alex Zhang
5 years ago

Unbelievable, this should be the default behavior.

Hessam
6 years ago

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

Berkay
11 years ago

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

Akash
4 years ago

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.

Guest
9 years ago

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
10 years ago

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
10 years ago

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
10 years ago

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

Ankur Raiyani
10 years ago
Reply to  Ankur Raiyani

Got the solutions.

Please use
@DynamicUpdate(value=true)

gmail
11 years ago

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

Dean
11 years ago

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

ashish
11 years ago

Could not find better explanation than this..thanx

Peter Ögren
11 years ago

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
11 years ago

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.

Alejandro
11 years ago

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 ?

Tez
13 years ago

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

Mike Hazelwood
13 years ago

Awesome article. Thanks!

Akhtar
13 years ago

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