×
☰ See All Chapters

@Column Annotation in JPA

Column annotation is used to map the variable of entity class to the column of database table. If it is omitted then it defaults to the field name. Column annotation has the following properties:

  1. String name: The column name. Defaults to the field name. 

  2. String columnDefinition: The SQL fragment that is used when generating the DDL for the column. This property is only used by vendors that support creating tables from annotations. During table creation, the vendor will use the value of the columnDefinition as the declared column type. If no columnDefinition is given, the vendor will choose an appropriate default based on the field type combined with the column's length, precision, and scale. 

  3. int length: The column length. (Applies only if a string-valued column is used. CHAR and VARCHAR columns) This property is typically only used during table creation, though some vendors might use it to validate data before flushing. CHAR and VARCHAR columns typically default to a length of 255; other column types use the database default. 

  4. int precision: he precision for a decimal (exact numeric) column. (Applies only if a decimal column is used.) Value must be set by developer if used when generating the DDL for the column. This property is often used in conjunction with scale to form the proper column type name during table creation. 

  5. int scale: The scale for a decimal (exact numeric) column. (Applies only if a decimal column is used.) This property is often used in conjunction with precision to form the proper column type name during table creation. 

  6. boolean nullable: Whether the column can store null values. Vendors may use this property both for table creation and at runtime; however, it is never required. Defaults to true. 

  7. boolean insertable: By setting this property to false, you can omit the column from SQL INSERT statements. Defaults to true. 

  8. boolean updatable: By setting this property to false, you can omit the column from SQL UPDATE statements. Defaults to true. 

  9. String table: Sometimes you will need to map fields to tables other than the primary table. This property allows you specify that the column resides in a secondary table. 

 


All Chapters
Author