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.


  1. annotation containing word "column" or "table" purely physical - properties only used control ddl/dml against database.

  2. other annotation purely logical - properties used in-memory in java control jpa processing.

  3. 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?

  1. 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.

  2. the following can safely implied examples provided in jpa spec

    • name & table can used in conjunction columndefinition, neither overridden
    • nullable overridden/made redundant columndefinition
  3. the following can safely implied "logic of situation" (did that?? :-p ):

    • length, precision, scale overridden/made redundant columndefinition - integral type
    • insertable , updateable provided separately , never included in columndefinition, because control sql generation in-memory, before emmitted database.
  4. 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

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -