Scrivio Interview library
InternalsIntermediate6 min read · updated 12 November 2026

How do @DynamicUpdate and Hibernate bytecode enhancement change dirty checking and the SQL that is generated? When would you use them?

By default Hibernate writes every column of an entity in its UPDATE, using one statement per entity prepared at startup, which keeps statement caching and batching simple. @DynamicUpdate makes Hibernate build an UPDATE with only the changed columns, which helps for wide tables and when concurrent transactions update different columns. Bytecode enhancement changes the entity classes at build time so they record which attributes were set, which lets Hibernate skip the full snapshot comparison, and it also enables lazy loading of basic attributes. Both have costs, so we use them where measurements show they help.

Why this question is asked

Most applications never touch these settings, and that is fine. But on a table with 80 columns, a service with thousands of managed entities per request, or a row that several processes update at the same time, the default behaviour can cost real CPU, network and lock contention. Interviewers ask this to see whether we know what Hibernate does at flush time and can reason about the trade-offs instead of switching options on by habit. In this answer, we will recap the default UPDATE, then look at what @DynamicUpdate changes, how bytecode enhancement changes dirty checking, and when each is worth its cost.

1A Quick Recap of the Default UPDATE

At flush, Hibernate's dirty checking compares every managed entity with the snapshot of values it took when the entity was loaded. If anything differs, it writes an UPDATE. By default that UPDATE sets every mapped column, not only the changed ones. Hibernate builds this statement once per entity type when the application starts and reuses it for every update of that type.

Java
@Entity class Customer { @Id Long id; String name; String email; String address; /* ... 40 more columns */ }
customer.setEmail("new@example.com");
// update customer set address=?, email=?, name=?, ... (all 43 columns) where id=?

Notice why this default is sensible. Because every update of a Customer is the same SQL text, the JDBC driver and the database can reuse one prepared statement, and Hibernate can put many updates of the same type into one JDBC batch. The cost is that we send and write columns that did not change.

2What @DynamicUpdate Changes

@DynamicUpdate, a Hibernate annotation on the entity class, tells Hibernate to build the UPDATE at flush time from the attributes that are actually dirty.

Java
@Entity
@DynamicUpdate
class Customer { /* same mapping */ }
customer.setEmail("new@example.com");
// update customer set email=? where id=?
customer.setName("Ana"); customer.setAddress("Main St 1");
// update customer set address=?, name=? where id=? (a different SQL text)

This helps in two situations. For wide tables, writing one column instead of forty saves network traffic, write work and, in some databases, index maintenance and log volume. For concurrency, two transactions that change different columns of the same row no longer overwrite each other's columns with stale values: with the default, transaction A writes its old copy of the email back while changing the name. @DynamicUpdate is also required for Hibernate's @OptimisticLocking(type = OptimisticLockType.DIRTY), which checks only the changed columns.

But there is a catch: each combination of changed columns is a different statement, so we lose part of the statement reuse and batching. Hibernate still needs the snapshot comparison to know which columns are dirty, so @DynamicUpdate makes the SQL smaller but does not make dirty checking cheaper.

3How Bytecode Enhancement Changes Dirty Checking

Bytecode enhancement means Hibernate rewrites the compiled entity classes, usually at build time with the Hibernate Maven or Gradle plugin, adding code to them. With dirty tracking enabled, each setter, or each field write, records the attribute name in a small tracker inside the entity. At flush, Hibernate asks the entity which attributes changed instead of comparing every attribute with the snapshot.

Java
// build.gradle
plugins { id 'org.hibernate.orm' version '<your Hibernate version>' }
hibernate {
enhancement {
enableDirtyTracking = true // option names vary by version; check your version
enableLazyInitialization = true
}
}
// at flush: customer.$$_hibernate_getDirtyAttributes() -> [email] (no full comparison)
Default, @DynamicUpdate and enhancement
AspectDefault@DynamicUpdateEnhanced dirty tracking
Columns in UPDATEallonly changedall, unless also @DynamicUpdate
How changes are foundfull snapshot comparefull snapshot comparetracked as fields are set
Statement reuseone per entityone per column combinationone per entity
Build impactnonenoneplugin in the build

So for a persistence context with 5,000 managed entities of which 3 changed, the default flush compares all attributes of 5,000 entities, while an enhanced flush asks 5,000 entities a cheap question and inspects only 3. That is the main CPU benefit.

4Lazy Loading of Basic Attributes

Enhancement also unlocks something the proxy mechanism cannot do: lazy loading of individual columns. Without enhancement, @Basic(fetch = FetchType.LAZY) is only a hint that Hibernate ignores, because there is no proxy for a String field. With enhancement, the enhanced getter can load the column on first access.

Java
@Entity
class Document {
@Id Long id;
String title;
@Basic(fetch = FetchType.LAZY) @Lob String body; // only lazy with enhancement
}
Document d = em.find(Document.class, 7L); // select id, title from document where id=7
d.getBody(); // select body from document where id=7

This is useful when an entity carries a large text or binary column that most screens never show, although a DTO projection or a separate entity for the large column often solves the same problem with less machinery.

5When Each Is Worth Its Cost

@DynamicUpdate costs nothing in the build, so the decision is about SQL. We would use it on wide tables where updates touch few columns, and on rows that different processes update in different columns. We would not put it on every entity, because narrow tables gain nothing and we give up some batching.

Enhancement costs build complexity: a plugin in every build, enhanced classes that behave slightly differently in tests and debuggers, and some features that change behaviour, for example associations and equality in enhanced classes. We would use it when profiling shows flush and dirty checking as a real share of CPU in write paths that hold many entities, or when we need lazy basic attributes. In both cases, the decision should follow a measurement: Hibernate statistics, statement logs and a profiler before and after.

6Follow-up questions

Does @DynamicInsert do the same for inserts?

Yes, in the same spirit. @DynamicInsert leaves out null columns from the INSERT, which lets database defaults apply to those columns. It has the same trade-off of more statement variants.

Can enhancement replace @Transactional(readOnly = true) for reads?

No. Read-only transactions stop snapshots and flushes completely for a read path, which is cheaper still. Enhancement reduces the cost of dirty checking in writing transactions, so the two solve different problems.

Is enhancement done at runtime too?

It can be, through a JPA class transformer when the container supports it, but build-time enhancement is more predictable and keeps startup fast, so it is the usual choice in Spring Boot applications.

7Conclusion

In this answer, we saw that Hibernate's default UPDATE writes all columns with one prepared statement per entity, which favours statement caching and batching. @DynamicUpdate writes only changed columns, which helps wide tables and concurrent column updates at the cost of more statement variants. Bytecode enhancement tracks changes as attributes are set, so flushes skip the full snapshot comparison, and it enables lazy basic attributes, at the cost of build complexity. We switch either on where measurements show a gain.

What to say in the interviewAbout 90 seconds aloud

Open with

By default Hibernate does two things that matter here. Dirty checking compares every attribute of every managed entity with the snapshot taken at load time, and when an entity is dirty, the UPDATE sets all its columns, using one statement per entity type prepared at startup.

Then, if they want more

That is a good default because the same SQL text can be cached and batched. DynamicUpdate changes the SQL: Hibernate builds the UPDATE at flush time with only the changed columns.

I would use it for wide tables where updates touch one or two columns, and where different processes update different columns of the same row, because then we stop writing back stale values for columns we did not touch. It is also needed for dirty-columns optimistic locking.

The cost is more statement variants and less batching, and it does not make dirty checking itself cheaper. Bytecode enhancement changes the classes at build time. With dirty tracking, setters record which attributes changed, so at flush Hibernate does not compare full snapshots, which helps when write paths hold thousands of managed entities.

Enhancement also makes lazy loading of basic attributes work, like a large text column. Its cost is a build plugin and slightly different runtime behaviour. So I would turn either on only after profiling or Hibernate statistics show the problem, and measure again afterwards.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How do @DynamicUpdate and Hibernate bytecode enhancement change dirty checking and the...": https://scrivio-interview-library.netlify.app/databases/jpa-fundamentals/06bbe5df81/