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 :
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.
My website mkyong.com is hosted by Liquid Web, I really like their fast and helpful support, it saved me a lot of time. Furthermore, their servers are extremely reliable, fast and stable, I don't remember when was the last time we had server downtime. Btw, they're offering $100 Coupon now, if you are sourcing for great hosting provider, go grab it.

Related Posts

Popular Posts