Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What properties does @Column columnDefinition make redundant?

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

like image 622
Bart van Heukelom Avatar asked Apr 18 '13 09:04

Bart van Heukelom


People also ask

What is the use of @column annotation?

@Column annotation is used for Adding the column the name in the table of a particular MySQL database.

Is @column a mandatory annotation?

@Column. Let's start with the @Column annotation. It is an optional annotation that enables you to customize the mapping between the entity attribute and the database column.

What is @column in hibernate?

The @Column annotation is defined as a part of the Java Persistence API specification. It's used mainly in the DDL schema metadata generation. This means that if we let Hibernate generate the database schema automatically, it applies the not null constraint to the particular database column.

What is @column in Java?

JPA JAVA EE. @Column Specifies the mapped column for a persistent property or field. If no Column annotation is specified, then the filed names will be used for mapping.


1 Answers

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.


  1. Annotation containing the word "Column" or "Table" is purely physical - properties only used to control DDL/DML against database.

  2. Other annotation purely logical - properties used in-memory in java to control JPA processing.

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

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

  2. The following can be safely implied from examples provided in the JPA spec

    • name & table can be used in conjunction with columnDefinition, neither are overridden
    • nullable is overridden/made redundant by columnDefinition
  3. 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 the columnDefinition - they are integral to the type
    • insertable and updateable are provided separately and never included in columnDefinition, because they control SQL generation in-memory, before it is emmitted to the database.
  4. 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
like image 82
Glen Best Avatar answered Oct 17 '22 23:10

Glen Best