Blog/Dirty checking without proxies

Dirty checking without proxies

Dirty checking is one of the nice things a traditional ORM does for you: it skips entities you did not change and can limit an UPDATE to the columns you did. The usual objection to value-based ORMs is that they give this up. ST/ORM keeps it.

March 17, 2026Internals4 min read

How the classic approach works

Hibernate has two ways to know what changed. It can snapshot every managed entity when it loads and, at flush time, diff the current state against that snapshot to build the UPDATE. Or it can enhance your bytecode so that setters record which fields were touched. Both are clever, and both need the session to own your objects, because the change tracking belongs to the managed lifecycle rather than to the data. That ownership is also where detached-state bugs and proxies that outlive their session come from.

How ST/ORM does it

ST/ORM compares the entity you write with the entity you read in the same transaction. If they match, it skips the UPDATE. If they differ, the default mode writes the row and field mode writes only the columns that changed. No proxy or bytecode enhancement is involved, and no session keeps a snapshot of your objects. The value observed at read time belongs to the transaction and is discarded when it ends. You read a User, produce an updated copy and save it, and ST/ORM works out the difference.

Because entities are immutable, an updated copy is the natural way to change one anyway, and it leaves the before and the after as two distinct values, which is what a diff needs. Immutability helps a second time when you save the same value you read. It is the same object, so reference identity settles the question before any field is compared, and nothing is written. When there is a change to inspect, ST/ORM reads the fields through the generated metamodel instead of by reflection, so the comparison goes through generated accessors with no runtime lookup in the path.

What that changes

You get the same minimal updates with a different cost structure. Change detection becomes a function of two values instead of a lifecycle you have to keep an entity inside. Outside a transaction an entity is plain data: nothing tracks it, nothing will flush it, and it cannot go stale the way a managed object can.