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

Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.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

  • Pingback: weblink()

  • Pingback: over here()

  • Pingback: alkaline water()

  • Pingback: water ionizer()

  • Pingback: loan payment plan()

  • Pingback: alkaline water brands()

  • Pingback: best minecraft adventure maps()

  • Pingback: electricians jacksonville()

  • Pingback: at bing()

  • Pingback: house blue()

  • Pingback: pay per day loans plan()

  • Pingback: ben 10 plumbers badge()

  • Pingback: plumbing code book 2006()

  • Pingback: locksmiths jacksonville nc()

  • Pingback: locksmith supplies wholesale()

  • Pingback: electrician training()

  • Pingback: electrician 101()

  • Pingback: water ionizer plans()

  • Pingback: pay per day loan plans()

  • Pingback: laan penge online()

  • Pingback: stop parking()

  • Pingback: laan penge nu og her 18 aar()

  • Pingback: car parking()

  • Pingback: lan hurtige penge nu()

  • Pingback: Blue Coaster33()

  • gmail

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

  • 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/&lt;project&gt;/src/main/webapp/},file:/C:/dev/&lt;project&gt;/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.&lt;init&gt;(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

    Could not find better explanation than this..thanx

  • 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

    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

    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

    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 ?

  • Pingback: Why do JPA-generated update queries include more fields than necessary? | PHP Developer Resource()

  • Tez

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

    • http://www.mkyong.com mkyong

      Hi Tez and Akhtar,

      Sorry for the typo error, article is updated. Thanks and appreciated your proof read.

  • Akhtar

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

  • Pingback: Dynamic-update with JPA | DeveloperQuestion.com()

  • Mike Hazelwood

    Awesome article. Thanks!

  • Pingback: Hibernate Tutorials | Tutorials()