为什么 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 ''.
我希望我说得有道理.特别是如果您正在考虑针对这种情况进行动态插入(以避免必须为非空列设置所有属性并依赖默认约束),请再考虑一下.
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屋!