@Column

From CauchoWiki

Jump to: navigation, search


The @Column annotation specifies the name of the database column for a persistent field or property. If the @Column is not specified the field name is used instead.

 @Entity
 public class TestBean {
    @Id
    @Column(name="t_id)
    public int getId()
     ...
 }

[edit] Annotation Definition

 public @interface Column {
     String name() default "";
     boolean unique() default false;
     boolean nullable() default true;
     boolean insertable() default true;
     boolean updatable() default true;
     boolean columnDefinition() default "";
     String table() default "";
     int length() default 255;
     int precision() default 0;
     int scale() default 0;
 }
attribute description default
name the database column name the property name
unique if true, the column is UNIQUE false
nullable if false, the column is NOT NULL true
insertable if true, the column is filled when the bean is created true
updatable if true, the column is updated when the bean changes true
columnDefinition SQL code for the column type at creation
table database table containing the column, e.g. a @SecondaryTable the entity's main table
length size of the column at table creation 255
precision precision of numeric types at table creation 0
scale scale of numeric types at table creation 0

Many applications will only use the name attribute. Most of the other annotations are only used if create-database-schema is true and the table does not exist in the database.

[edit] Related annotations

  • @Basic annotation for a primitive-valued column
  • @Id annotation for a primary key
  • @Table specifies database table
  • @JoinColumn specifies column names for foreign keys

[edit] Example

 @Entity
 public class TestBean {
     @Id
     @Column(name="t_id")
     private int id;

     @Basic
     @Column(name="t_data",length=32)
     private String data;
 }
 create table TESTBEAN (
     t_id integer primary key,

     t_data varchar(32)
 )
Personal tools