Skip to content
Advertisement

Why does not Hibernate set @DynamicInsert by default

Could anyone explain to me why Hibernate does not set the @DynamicInsert annotation by default and allow entities to generate an INSERT based on the currently set properties?

What’s the reason for not using @DynamicInsert and, therefore, including all entity properties by default?

Advertisement

Answer

What @jb-nizet said.

Also, dynamic-insert="true" is a bad idea in my book.

Skipping null fields from the generated SQL, soon you will find yourself in a situation where you will be declaring columns not null default which in fact causes the persistent data to be different than the entity data that hibernate knows about. This will cause frustration and likely make you resort to expensive session.refresh() calls.

For example suppose column

MESSAGE varchar(64) not null default ''

and you save an entity with null value for the property that is mapped to this column.

With dynamic-insert you will end up with an entity that has null value for the property message in-memory while at the same time the corresponding database row has ”.

I hope I made sense. Especially if you are considering dynamic-insert for such a scenario (to get away from having to set all properties for not null columns and rely on a default constraint), think again.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement