Rule Definition
Triggers and object-relational mapping often create issues due to the difficulties of synchronizing the effect of a trigger with the representation of data in memory.
When you are using detached objects associated to a table associated to trigger, Hibernate may execute unnecessary SQL UPDATE statements to ensure that the database state is synchronized with the session state and will affect the performances of your application.
Remediation
Use the attribute select-before-update for the persistent class associated to a table that have the UPDATE trigger. This setting forces Hibernate to retrieve a snapshot of the current database state using an SQL SELECT that will enable the subsequent UPDATE to be avoided when the state of the persistent object in the memory is the same.
Violation Code Sample
SAMPLE is a table managed by an UPDATE trigger
---> sample.hbm.xml
// VIOLATION
<class name="Sample" table="SAMPLE">
...
</class>
Fixed Code Sample
---> sample.hbm.xml
// FIXED
<class name="Sample" table="SAMPLE"
select-before-update="true">
...
</class>
Reference
Hibernate in Action (ISBN 1932394-15-X) p 339
Java Persistence with Hibernate (ISBN 1-932394-88-5) p 349
Related Technologies
Technical Criterion
Efficiency - SQL and Data Handling Performance
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.