What properties of @Column columnDefinition make redundant?

I often quote my @Column annotations as follows:

 @Column(columnDefinition="character varying (100) not null",length=100,nullable=false) 

As you can see, I specify length and nullable , although columnDefinition already indicates them. This is because I do not know where / when these values ​​are used accurately.

So, when specifying columnDefinition , what other @Column properties become redundant?

If that matters, I use Hibernate and PostgreSQL

+33
java hibernate jpa
Apr 18 '13 at 9:04 on
source share
2 answers

My answer: all of the following should be redefined (i.e. describe them all in columndefinition , if necessary):

  • length
  • precision
  • scale
  • nullable
  • unique

i.e. a DDL column will consist of: name + columndefinition and nothing else.

The rationale follows.




  • Annotations containing the word "Column" or "Table" are purely physical properties that are used only for managing DDL / DML with the database.

  • Other notes are purely logical - properties used in memory in java to control JPA processing.

  • That's why it sometimes seems that the / nullability option is set twice - once through @Basic(...,optional=true) and once through @Column(...,nullable=true) . The former says that the attribute / association may be null in the JPA object model (in memory) during cleanup; the latter says the DB column may be null. Usually you want them to install the same, but not always, depending on how the database tables are installed and reused.

In your example, the length and nullable properties are overridden and redundant.




So, when specifying columnDefinition do other @Column properties become redundant?

  • In JPA Spec and javadoc:

    • columndefinition definition: An SQL fragment that is used when generating DDL for a column.

    • columndefinition default: Generated SQL to create a column of the output 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, I’m wrong ... this is really so .: - $ ?!

    Does columnDefinition indicate other properties provided in a single annotation?

    The javadoc and JPA specification does not explain this explicitly - the specification does not provide much protection. To be 100% sure, test your chosen implementation.

  • From the examples given in the JPA specification

    we can safely mean the following:
    • name and table can be used with columndefinition and they are not overridden
    • nullable overridden / reserved with columndefinition
  • From the "logic of the situation" we can rightly do the following: (I just said that ??: -P):

    • length , precision , scale overridden / made redundant with columndefinition - they are an integral part of type
    • insertable and updateable provided separately and are never included in columndefinition , since they control the generation of SQL in memory before it is sent to the database.
  • This leaves the property " unique ". It is similar to nullable - it extends / qualifies a type definition, therefore it should be considered as an integral type definition. that is, it must be redefined.




Check my answer For columns "A" and "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" 
  • confirmation of the generated table has the correct type / validity / uniqueness
  • optionally insert and update JPA: the former should include column A, the last column B
+48
Jul 05 '13 at 6:09
source share

columnDefinition will override the sql DDL generated by hibernating for this column; it is not portable and depends on the database used. You can use it to indicate NULL, length, precision, scale ... ect.

+5
Jul 04 '13 at 14:11
source share



All Articles