为什么默认情况下Hibernate不设置@DynamicInsert [英] Why does not Hibernate set @DynamicInsert by default
问题描述
有人可以向我解释为什么Hibernate默认不设置@DynamicInsert
批注,并允许实体根据当前设置的属性生成INSERT吗?
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?
为什么不使用@DynamicInsert
并因此默认情况下包括所有实体属性的原因是什么?
What's the reason for not using @DynamicInsert
and, therefore, including all entity properties by default?
推荐答案
@ jb-nizet所说的话.
What @jb-nizet said.
此外,dynamic-insert="true"
在我的书中不是一个好主意.
Also, dynamic-insert="true"
is a bad idea in my book.
跳过生成的SQL中的空字段,很快您将发现自己在声明not null default
列的情况,这实际上导致持久性数据不同于休眠所了解的实体数据.这会造成挫败感,并可能使您诉诸昂贵的session.refresh()
通话.
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.
例如,假设列
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 ''.
我希望我有道理.尤其是如果您正在考虑针对这种情况进行动态插入(以摆脱不必为非null列设置所有属性并依赖默认约束的情况),请三思.
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.
这篇关于为什么默认情况下Hibernate不设置@DynamicInsert的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!