@Column columnDefinition具有多少属性? [英] What properties does @Column columnDefinition make redundant?
问题描述
我经常指定我的 @Column
这样的注释:
@Column(columnDefinition =character varying(100)not null,length = 100,nullable = false)
正如你所看到的,即使 columnDefinition $>指定了
length
和 nullable
c $ c>已经指定了这些。这是因为我不知道这些值的确切位置/时间。
columnDefinition
时, @Column
的其他属性是多余的吗? 如果有问题,我使用Hibernate和PostgreSQL
columndefinition
$ b
-
长度
-
精确
-
比例
-
可为空
-
unique
即DDL的列将包含: name
+ columndefinition
,而不是别的。
理由如下。
-
包含单词Column或Table的注释纯粹是物理的 - 仅用于控制数据库DDL / DML的 。
其他注释purely logical - 在java中用于控制JPA处理的内存属性。 -
这就是为什么有时看起来可选性/可空性设置了两次 - 一次通过
@Basic(...,可选= true)
,一次通过@Column(...,nullable = true)
。前者说在冲洗时,JPA对象模型(内存中)中的属性/关联可以为null;后者说DB列可以为null。通常你会希望它们设置相同的,但并非总是,这取决于数据库表的设置和重用情况。
在您的示例中,长度和可为空的属性被覆盖并且是多余的。
所以,当指定columnDefinition时,@还有其他的属性@列是多余的?
-
在JPA Spec& amp ; javadoc:
-
columnDefinition
定义:
生成列的DDL时使用的SQL片段。
默认值: -
-
错......,就是这样。 : - $?!
-
名称
&table
可以与columnDefinition
一起使用,不会被覆盖 -
nullable
以下内容可以从情境逻辑中相当安全地暗示出来(我刚刚说过:??:-P):
-
长度
,精确
,scale
被columnDefinition
覆盖/冗余 - 它们是类型的一部分 -
可插入和
,因为它们在内存中控制SQL生成,然后将其转移到数据库中。可更新
是单独提供的, code> columnDefinition
> -
只留下
唯一
属性。它类似于可空 - 扩展/限定类型定义,因此应该将其视为整型以定义类型。即应该被覆盖。
生成的SQL来创建推断类型的列。 提供以下示例: p>
@Column(name =DESC,columnDefinition =CLOB NOT NULL,table =EMP_DETAIL)
@列(name =EMP_PIC,columnDefinition =BLOB NOT NULL)
columnDefinition是否会覆盖同一批注中提供的其他属性?
javadoc和JPA规范没有明确地解决这个问题 - spec没有提供很好的保护。为了100%确定,请使用您选择的实现进行测试。 JPA规范中提供的示例可以安全地隐含以下内容。 b
通过 columnDefinition
$ b覆盖/重复
测试我的答案
对于列A& B分别是:
@Column(name =...,table =...,可插入= true,updateable = false,
columndefinition =NUMBER(5,2)NOT NULL UNIQUE
@Column(name =...,table =... ,insertable = false,updateable = true,
columndefinition =NVARCHAR2(100)NULL
- 确认生成的表具有正确的类型/可空性/唯一性
- 可选地,JPA插入& update:前者应该包括列A,后列B
I often specify my @Column
annotations like this:
@Column(columnDefinition="character varying (100) not null",length=100,nullable=false)
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.
So, when specifying columnDefinition
, what other properties of @Column
are made redundant?
If it matters, I use Hibernate and PostgreSQL
My Answer: All of the following should be overridden (i.e. describe them all within columndefinition
, if appropriate):
length
precision
scale
nullable
unique
i.e. the column DDL will consist of: name
+ columndefinition
and nothing else.
Rationale follows.
Annotation containing the word "Column" or "Table" is purely physical - properties only used to control DDL/DML against database.
Other annotation purely logical - properties used in-memory in java to control JPA processing.
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.
So, when specifying columnDefinition, what other properties of @Column are made redundant?
In JPA Spec & javadoc:
columnDefinition
definition: The SQL fragment that is used when generating the DDL for the column.columnDefinition
default: Generated SQL to create a column of the inferred type.The following examples are provided:
@Column(name="DESC", columnDefinition="CLOB NOT NULL", table="EMP_DETAIL") @Column(name="EMP_PIC", columnDefinition="BLOB NOT NULL")
And, err..., that's it really. :-$ ?!
Does columnDefinition override other properties provided in the same annotation?
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.
The following can be safely implied from examples provided in the JPA spec
name
&table
can be used in conjunction withcolumnDefinition
, neither are overriddennullable
is overridden/made redundant bycolumnDefinition
The following can be fairly safely implied from the "logic of the situation" (did I just say that?? :-P ):
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.
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.
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"
- confirm generated table has correct type/nullability/uniqueness
- optionally, do JPA insert & update: former should include column A, latter column B
这篇关于@Column columnDefinition具有多少属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!