目录

导读

是什么?

步骤

总结


Java 注解(Annotation)又称 Java 标注,是 JDK5.0 引入的一种注释机制。Java 语言中的类、方法、变量、参数和包等都可以被标注。和 Javadoc 不同,Java 标注可以通过反射获取标注内容。在编译器生成类文件时,标注可以被嵌入到字节码中。Java 虚拟机可以保留标注内容,在运行时可以获取到标注内容 。 当然它也支持自定义 Java 标注。

@Column标记表示所持久化属性所映射表中的字段


 

@Target(value={METHOD,FIELD})  @Retention(value=RUNTIME) public @interface Column Specifies the mapped column for a persistent property or field. If no Column annotation is specified, the default values apply.     Example 1:      @Column(name="DESC", nullable=false, length=512)     public String getDescription() { return description; }      Example 2:      @Column(name="DESC",             columnDefinition="CLOB NOT NULL",             table="EMP_DETAIL")     @Lob     public String getDescription() { return description; }      Example 3:      @Column(name="ORDER_COST", updatable=false, precision=12, scale=2)     public BigDecimal getCost() { return cost; }
@Target({METHOD, FIELD}) @Retention(RUNTIME)  public @interface Column {  String name() default "";  boolean unique() default false;  boolean nullable() default true;  boolean insertable() default true;  boolean updatable() default true;  String columnDefinition() default "";  String table() default "";  int length() default 255;  int precision() default 0;  int scale() default 0;  }

@Column属性详解:@Column注解一共有10个属性,这10个属性均为可选属性

name 
name属性定义了被标注字段在数据库表中所对应字段的名称;

unique 
unique属性表示该字段是否为唯一标识,默认为false。如果表中有一个字段需要唯一标识,则既可以使用该标记,也可以使用@Table标记中的@UniqueConstraint。

nullable 
nullable属性表示该字段是否可以为null值,默认为true。

insertable 
insertable属性表示在使用“INSERT”脚本插入数据时,是否需要插入该字段的值。

updatable 
updatable属性表示在使用“UPDATE”脚本插入数据时,是否需要更新该字段的值。insertable和updatable属性一般多用于只读的属性,例如主键和外键等。这些字段的值通常是自动生成的。

columnDefinition 
columnDefinition属性表示创建表时,该字段创建的SQL语句,一般用于通过Entity生成表定义时使用。(也就是说,如果DB中表已经建好,该属性没有必要使用。)

table 
table属性定义了包含当前字段的表名。

length 
length属性表示字段的长度,当字段的类型为varchar时,该属性才有效,默认为255个字符。

precision和scale 
precision属性和scale属性表示精度,当字段类型为double时,precision表示数值的总长度,scale表示小数点所占的位数。

 @Column可以标注在属性前或getter方法前;


以上就是今天要讲的内容,本文系统介绍了@Column属性注解

 

 

参考文章:http://docs.oracle.com/javaee/5/api/javax/persistence/Column.html