@Column columnDefinition 使哪些属性变得多余? [英] What properties does @Column columnDefinition make redundant?
问题描述
我经常像这样指定我的 @Column
注释:
I often specify my @Column
annotations like this:
@Column(columnDefinition="character varying (100) not null",length=100,nullable=false)
如您所见,我指定了 length
和 nullable
,即使 columnDefinition
已经指定了它们.那是因为我不知道这些值的确切使用地点/时间.
As you can see I specify length
and nullable
even though the columnDefinition
already specifies those. That's because I don't know where/when these values are used exactly.
那么,当指定columnDefinition
时,@Column
的其他哪些属性是多余的?
So, when specifying columnDefinition
, what other properties of @Column
are made redundant?
如果有关系,我使用 Hibernate 和 PostgreSQL
If it matters, I use Hibernate and PostgreSQL
推荐答案
我的回答:以下所有内容都应该被覆盖(即在columndefinition
中描述它们,如果合适的话):
My Answer: All of the following should be overridden (i.e. describe them all within columndefinition
, if appropriate):
长度
精度
比例
可为空
独特
即列 DDL 将包括:name
+ columndefinition
没有别的.
i.e. the column DDL will consist of: name
+ columndefinition
and nothing else.
基本原理如下.
包含列"或表"一词的注释纯粹是物理属性 - 仅用于控制针对数据库的 DDL/DML.
Annotation containing the word "Column" or "Table" is purely physical - properties only used to control DDL/DML against database.
其他纯逻辑注释 - 在 Java 内存中用于控制 JPA 处理的属性.
Other annotation purely logical - properties used in-memory in java to control JPA processing.
这就是为什么有时会出现可选性/可空性设置两次 - 一次通过 @Basic(...,optional=true)
和一次通过 @Column(...,nullable=true)
.前者说在刷新时,JPA 对象模型(内存中)中的属性/关联可以为空;后者说 DB 列可以为空.通常,您希望它们设置相同 - 但并非总是,具体取决于数据库表的设置和重用方式.
That's why sometimes it appears the optionality/nullability is set twice - once via @Basic(...,optional=true)
and once via @Column(...,nullable=true)
. Former says attribute/association can be null in the JPA object model (in-memory), at flush time; latter says DB column can be null. Usually you'd want them set the same - but not always, depending on how the DB tables are setup and reused.
在您的示例中,长度和可为空的属性被覆盖和冗余.
In your example, length and nullable properties are overridden and redundant.
那么,在指定 columnDefinition 时,@Column 的其他哪些属性是多余的?
在 JPA 规范中文档:
In JPA Spec & javadoc:
columnDefinition
定义:为列生成 DDL 时使用的 SQL 片段.
columnDefinition
默认:生成的 SQL 以创建推断类型的列.
提供了以下示例:
@Column(name="DESC", columnDefinition="CLOB NOT NULL", table="EMP_DETAIL")
@Column(name="EMP_PIC", columnDefinition="BLOB NOT NULL")
而且,呃……真的是这样.:-$ ?!
And, err..., that's it really. :-$ ?!
columnDefinition 是否会覆盖同一注释中提供的其他属性?
javadoc 和 JPA 规范没有明确解决这个问题 - 规范没有提供很好的保护.为了 100% 确定,请使用您选择的实现进行测试.
The javadoc and JPA spec don't explicity address this - spec's not giving great protection. To be 100% sure, test with your chosen implementation.
可以从 JPA 规范中提供的示例中安全地暗示以下内容
The following can be safely implied from examples provided in the JPA spec
name
&table
可以与columnDefinition
结合使用,两者都不会被覆盖nullable
被columnDefinition
覆盖/冗余
name
&table
can be used in conjunction withcolumnDefinition
, neither are overriddennullable
is overridden/made redundant bycolumnDefinition
从情况的逻辑"中可以相当安全地暗示以下内容(我刚刚说过吗??:-P):
The following can be fairly safely implied from the "logic of the situation" (did I just say that?? :-P ):
length
、precision
、scale
被columnDefinition
覆盖/冗余 - 它们是类型的整体insertable
和updateable
是分开提供的,从不包含在columnDefinition
中,因为它们控制内存中的 SQL 生成,在它之前发送到数据库.
length
,precision
,scale
are overridden/made redundant by thecolumnDefinition
- they are integral to the typeinsertable
andupdateable
are provided separately and never included incolumnDefinition
, because they control SQL generation in-memory, before it is emmitted to the database.
只剩下unique
"属性.它类似于可为空 - 扩展/限定类型定义,因此应将其视为类型定义的整体.即应该被覆盖.
That leaves just the "unique
" property. It's similar to nullable - extends/qualifies the type definition, so should be treated integral to type definition. i.e. should be overridden.
<小时>
测试我的答案对于列A"和B"分别为:
Test My Answer For columns "A" & "B", respectively:
@Column(name="...", table="...", insertable=true, updateable=false,
columndefinition="NUMBER(5,2) NOT NULL UNIQUE"
@Column(name="...", table="...", insertable=false, updateable=true,
columndefinition="NVARCHAR2(100) NULL"
- 确认生成的表具有正确的类型/可空性/唯一性
- 可选地,执行 JPA 插入 &更新:前者应包含 A 列,后者应包含 B 列
这篇关于@Column columnDefinition 使哪些属性变得多余?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!