Quick revision
The short answer
@Immutable tells Hibernate that an entity never changes after it is created, so Hibernate skips dirty checking for it and never writes UPDATE statements; changes made to the Java object are simply ignored at flush, while persist and delete still work. We use it for reference data, for database views or @Subselect queries mapped as entities, and for append-only records such as events or ledger entries. Because Hibernate keeps no snapshot for immutable entities, it also saves memory and CPU, and read-only sessions, queries or @Transactional(readOnly = true) give similar savings for a single unit of work without marking the entity.
What to remember
In this answer, we saw that @Immutable tells Hibernate an entity never changes, so it keeps no snapshot, skips dirty checking and never writes an UPDATE, while persist and delete still work and in-memory changes are silently ignored at flush, with a warning in recent versions. It fits reference data, views and @Subselect mappings, and event or ledger records, and it saves memory and flush time. Read-only transactions and queries give similar savings per unit of work when the entity itself can change.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
@Immutable tells Hibernate that an entity never changes after it is created, so Hibernate skips dirty checking for it and never writes UPDATE statements; changes made to the Java object are simply ignored at flush, while persist and delete still work. We use it for reference data, for database views or @Subselect queries mapped as entities, and for append-only records such as events or ledger entries. Because Hibernate keeps no snapshot for immutable entities, it also saves memory and CPU, and read-only sessions, queries or @Transactional(readOnly = true) give similar savings for a single unit of work without marking the entity.
Why this question is asked
Some data in every system is written once and then only read: country codes, currency tables, audit events, ledger lines, rows from a reporting view. Hibernate treats all entities as changeable by default, which means it spends memory on snapshots and CPU on dirty checks for data that never changes, and it allows code to modify records that should be untouchable. Interviewers ask about @Immutable to see whether we know how to express that intent and what it actually does at runtime. In this answer, we will see what @Immutable changes, what happens when code tries to modify such an entity, where it fits, what it saves, and how read-only modes compare.
1What @Immutable Changes
org.hibernate.annotations.Immutable is a Hibernate annotation placed on an entity class. It tells Hibernate that instances are never updated. Hibernate therefore loads them as read-only: it keeps no snapshot of their loaded values, does not include them in dirty checking at flush, and never generates an UPDATE for them. Inserting new instances with persist() and deleting them with remove() still work, because immutable means the state of an existing row does not change, not that the table is frozen.
2What Happens When Code Tries to Change One
If code calls a setter on an immutable entity, the Java object changes, but Hibernate ignores the change at flush. No exception is thrown and no UPDATE is sent; recent Hibernate versions log a warning so the mistake is visible. Bulk JPQL updates are different, because they bypass entities: depending on the hibernate.query.immutable_entity_update_query_handling_mode setting, Hibernate logs a warning or throws an exception, and the default differs between versions, so check your version and set it to exception if you want a hard stop.
- Flush starts
- Entity class is @Immutable?Yesskip: no snapshot, no dirty checkNocompare with the snapshot
- Values differ from the snapshot?Yeswrite an UPDATENonothing to write
3 steps in order. Play them, or step through them one at a time.
Notice the subtle risk in the first case: within the same transaction, code that reads e.getAmount() sees 999.00, which the database will never hold. Immutable entities should therefore also be written as immutable Java classes where possible, with no setters, so the mistake cannot be made at all.
3Where @Immutable Fits
Reference data is the classic case: countries, currencies, tax categories, loaded constantly and changed only by migrations. Marking them immutable removes their cost from every flush, and they are good candidates for the second-level cache too, where immutable entities can use the simplest read-only cache strategy.
Database views mapped as entities are another case. A view such as order_summary has no single table to update, so writes would fail anyway; @Immutable makes that explicit. Hibernate's @Subselect maps an entity to a SQL query instead of a table, and @Synchronize lists the tables it reads so Hibernate flushes pending changes to them before querying the entity.
The third case is append-only records: domain events, audit records and ledger entries, where changing history is not allowed by design. Here @Immutable documents the rule and stops accidental updates through the ORM, though the real protection for financial data belongs in the database too, for example by granting the application no UPDATE privilege on the table.
4What It Saves
For a normal entity, Hibernate keeps two copies of its state: the object and the snapshot used for dirty checking. For immutable entities it keeps only the object, which roughly halves the memory those entities use in the persistence context, and it skips them entirely during each flush. If a request loads 5,000 reference rows, that is 5,000 fewer snapshots and 5,000 fewer comparisons at every flush in that transaction.
5Read-Only Modes: The Per-Session Alternative
Sometimes the entity itself is changeable, but one use case only reads it. Then we do not mark the class; we make the unit of work read-only. @Transactional(readOnly = true) in Spring, with Hibernate, sets the session's flush mode to manual and its default to read-only, so loaded entities get no snapshots. We can also mark a single query read-only with the org.hibernate.readOnly hint, or call session.setDefaultReadOnly(true) directly.
| Aspect | @Immutable entity | Read-only transaction or query |
|---|---|---|
| Scope | every use of the entity | one transaction or one query |
| Snapshots and dirty checks | never | not for entities loaded read-only |
| Insert and delete | allowed | the transaction should not write |
| Good for | reference data, views, ledgers | read paths of changeable entities |
So the choice is about where the truth lives. If the data never changes for anyone, @Immutable on the entity says so once, for every use. If it changes in some use cases and not in others, read-only transactions and queries give the same savings exactly where they apply.
6Follow-up questions
Is @Immutable part of standard JPA?
No. It is a Hibernate annotation, org.hibernate.annotations.Immutable. Standard JPA has no equivalent on the entity, although marking columns insertable = true and updatable = false gets part of the effect for individual columns.
Can collections be immutable too?
Yes. @Immutable on a collection mapping makes Hibernate reject adding or removing elements, and in that case it throws an exception rather than silently ignoring the change.
Does @Immutable make the Java object immutable?
No. It only changes how Hibernate persists the entity. The Java object can still be modified in memory, which is why we prefer classes without setters for immutable entities.
7Conclusion
In this answer, we saw that @Immutable tells Hibernate an entity never changes, so it keeps no snapshot, skips dirty checking and never writes an UPDATE, while persist and delete still work and in-memory changes are silently ignored at flush, with a warning in recent versions. It fits reference data, views and @Subselect mappings, and event or ledger records, and it saves memory and flush time. Read-only transactions and queries give similar savings per unit of work when the entity itself can change.
Open with
Immutable is a Hibernate annotation that tells Hibernate an entity is never updated after it is inserted. Because of that, Hibernate loads it read-only: it keeps no snapshot of the loaded values, leaves it out of dirty checking, and never generates an UPDATE.
Then, if they want more
You can still persist new instances and delete existing ones. If code calls a setter on one, the change is ignored at flush, no update is sent, and recent versions log a warning, which is a bit dangerous because the in-memory object now differs from the database. That is why I like immutable entities to have no setters at all.
Bulk JPQL updates against them either warn or throw, depending on a Hibernate setting whose default you should check. I use it for three kinds of data. Reference data like countries or currencies, which is loaded constantly and only changed by migrations, and which also caches well. Database views or Subselect mappings, where updates make no sense.
And append-only records like domain events or ledger entries, where changing history is forbidden, although for financial data I would also remove UPDATE privileges in the database. The benefit is memory and CPU: no snapshots and no comparisons at flush.
If the entity can change in general but a particular use case only reads, I would not mark the class; I would use Transactional readOnly true, or the read-only query hint, which gives similar savings just for that unit of work.
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.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
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 "What does @Immutable do on a Hibernate entity, and when would you use it?": https://scrivio-interview-library.netlify.app/databases/jpa-fundamentals/e326a49981/