java - What properties does @Column columnDefinition make redundant? -
i specify @column
annotations this:
@column(columndefinition="character varying (100) not null",length=100,nullable=false)
as can see specify length
, nullable
though columndefinition
specifies those. that's because don't know where/when these values used exactly.
so, when specifying columndefinition
, other properties of @column
made redundant?
if matters, use hibernate , postgresql
my answer: of following should overridden (i.e. describe them within columndefinition
, if appropriate):
length
precision
scale
nullable
unique
i.e. column ddl consist of: name
+ columndefinition
and nothing else.
rationale follows.
annotation containing word "column" or "table" purely physical - properties only used control ddl/dml against database.
other annotation purely logical - properties used in-memory in java control jpa processing.
that's why appears optionality/nullability set twice - once via
@basic(...,optional=true)
, once via@column(...,nullable=true)
. former says attribute/association can null in jpa object model (in-memory), @ flush time; latter says db column can null. you'd want them set same - but not always, depending on how db tables setup , reused.
in example, length , nullable properties overridden , redundant.
so, when specifying columndefinition, other properties of @column made redundant?
in jpa spec & javadoc:
columndefinition
definition: the sql fragment used when generating ddl column.columndefinition
default: generated sql create column of inferred type.the following examples provided:
@column(name="desc", columndefinition="clob not null", table="emp_detail") @column(name="emp_pic", columndefinition="blob not null")
and, err..., that's really. :-$ ?!
does columndefinition override other properties provided in same annotation?
the javadoc , jpa spec don't explicity address - spec's not giving great protection. 100% sure, test chosen implementation.
the following can safely implied examples provided in jpa spec
name
&table
can used in conjunctioncolumndefinition
, neither overriddennullable
overridden/made redundantcolumndefinition
the following can safely implied "logic of situation" (did that?? :-p ):
length
,precision
,scale
overridden/made redundantcolumndefinition
- integral typeinsertable
,updateable
provided separately , never included incolumndefinition
, because control sql generation in-memory, before emmitted database.
that leaves "
unique
" property. it's similar nullable - extends/qualifies type definition, should treated integral type definition. i.e. should overridden.
test answer 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, jpa insert & update: former should include column a, latter column b
Comments
Post a Comment