Hibernate Dynamic insert and Dynamic update
1.
Dynamic insert: In hibernate by default dynamic
insert attribute is false it means when an object is inserted to the database
it will include all columns whose values are null i.e. it will generate an
insert statement with all unnecessary columns. For example see below code.
Stock
stockTran = new Stock ();
//stockTran.setPriceOpen(new
Float("1.2"));
//stockTran.setPriceClose(new
Float("1.1"));
//stockTran.setPriceChange(new
Float("10.0"));
stockTran.setVolume(2000000L);
stockTran.setDate(new
Date());
stockTran.setStock(stock);
session.save(stockTran);
make show_sql
true in hibernate configuration and you will see following insert statement
generated by Hibernate SQL.
INSERT
INTO
stock
(DATE, PRICE_CHANGE, PRICE_CLOSE, PRICE_OPEN, STOCK_ID, VOLUME)
VALUES
(?, ?, ?, ?, ?, ?)
Hibernate will generate the unnecessary
columns (PRICE_CHANGE, PRICE_CLOSE, PRICE_OPEN) for the insertion.
Now make dynamic insert attribute true and
try to insert the same stock object data and you will see that hibernate insert
statement excludes all the null property values.
Now hibernate generates the following insert
statement.
INSERT
INTO
stock
(DATE, STOCK_ID, VOLUME)
VALUES
(?, ?, ?)
Hibernate will generate only
the necessary columns (DATE, STOCK_ID, VOLUME) for the
insertion.
The main concern is performance issue
In some situations, such as a very large
table with hundreds of columns (legacy design), or a table contains extremely
large data volume, insert something not necessary definitely will drop down
your system performance. So it is highly recommended to use the Dynamic Insert
attribute true.
a.
Dynamic insert configuration using Annotation
@Entity
@Table(name = "stock ")
@org.hibernate.annotations.Entity(
dynamicInsert = true
)
public class Stock implements java.io.Serializable {}
b.
Dynamic insert configuration using XML
<class ... table="stock " dynamic-insert="true">
<id name="stockId" type="java.lang.Integer">
<column name="STOCK_ID" />
</id>
2.
Dynamic Update: By default hibernate set this
attribute false means when we updated an object to the database using hibernate
it generate the update SQL for all columns i.e. also for the columns those are
not modified due to this update statement we face the system performance issue
if we are playing with a table which has many columns or more data.
For example see the below code.
Query q = session.createQuery("from Stock where stockId = : stockId");
q.setParameter("stockId ", 11);
Stock stock = (Stock)q.list().get(0);
stock.setVolume(4000000L);
session.update(stock);
Hibernate will generate the following update statement
UPDATE
stock
SET
DATE=?,
PRICE_CHANGE=?,
PRICE_CLOSE=?,
PRICE_OPEN=?,
STOCK_ID=?,
VOLUME=?
WHERE
STOCK_ID=?
That is hibernate will update all the unmodified columns.
But when we make dynamic update attribute true which means all the unmodified columns will be excluded from the updated statement.
For the above written update code hibernate will generate the following sql.
UPDATE
stock
SET
VOLUME=?
WHERE
STOCK_ID=?
That is hibernate will updated the modified columns only.
Performance Concern:
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. So we should use the
dynamic update attribute true.
a.
Dynamic update configuration using Annotation
@Entity
@Table(name = "stock ")
@org.hibernate.annotations.Entity(
dynamicUpdate = true
)
public class Stock implements java.io.Serializable {}
b.
Dynamic update configuration using XML
<class ... table="stock " dynamic-update="true">
<id name="stockId" type="java.lang.Integer">
<column name="STOCK_ID" />
//add generator
</id>