@Column注解
用来标识实体类中属性与数据表中字段的对应关系
(1)源码:
/* * Copyright (c) 2008, 2009, 2011 Oracle, Inc. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 * which accompanies this distribution. The Eclipse Public License is available * at http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution License * is available at http://www.eclipse.org/org/documents/edl-v10.php. */ package javax.persistence; import java.lang.annotation.Retention; import java.lang.annotation.Target; import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.RetentionPolicy.RUNTIME; /** * Is used to specify the mapped column for a persistent property or field. * If no <code>Column</code> annotation is specified, the default values apply. * * <blockquote><pre> * 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; } * * </pre></blockquote> * * * @since Java Persistence 1.0 */ @Target({METHOD, FIELD}) @Retention(RUNTIME) public @interface Column { /** * (Optional) The name of the column. Defaults to * the property or field name. */ String name() default ""; /** * (Optional) Whether the column is a unique key. This is a * shortcut for the <code>UniqueConstraint</code> annotation at the table * level and is useful for when the unique key constraint * corresponds to only a single column. This constraint applies * in addition to any constraint entailed by primary key mapping and * to constraints specified at the table level. */ boolean unique() default false; /** * (Optional) Whether the database column is nullable. */ boolean nullable() default true; /** * (Optional) Whether the column is included in SQL INSERT * statements generated by the persistence provider. */ boolean insertable() default true; /** * (Optional) Whether the column is included in SQL UPDATE * statements generated by the persistence provider. */ boolean updatable() default true; /** * (Optional) The SQL fragment that is used when * generating the DDL for the column. * <p> Defaults to the generated SQL to create a * column of the inferred type. */ String columnDefinition() default ""; /** * (Optional) The name of the table that contains the column. * If absent the column is assumed to be in the primary table. */ String table() default ""; /** * (Optional) The column length. (Applies only if a * string-valued column is used.) */ int length() default 255; /** * (Optional) The 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. */ int precision() default 0; /** * (Optional) The scale for a decimal (exact numeric) column. * (Applies only if a decimal column is used.) */ int scale() default 0; }
(2)@Column属性详解:
name
定义了被标注字段在数据库表中所对应字段的名称;
unique
表示该字段是否为唯一标识,默认为false。如果表中有一个字段需要唯一标识,则既可以使用该标记,也可以使用@Table标记中的@UniqueConstraint。
nullable
表示该字段是否可以为null值,默认为true。
insertable
表示在使用“INSERT”脚本插入数据时,是否需要插入该字段的值。
updatable
表示在使用“UPDATE”脚本插入数据时,是否需要更新该字段的值。insertable和updatable属性一般多用于只读的属性,例如主键和外键等。这些字段的值通常是自动生成的。
columnDefinition(大多数情况,几乎不用)
表示创建表时,该字段创建的SQL语句,一般用于通过Entity生成表定义时使用。(也就是说,如果DB中表已经建好,该属性没有必要使用。)
table
表示当映射多个表时,指定表的表中的字段。默认值为主表的表名。
length
表示字段的长度,当字段的类型为varchar时,该属性才有效,默认为255个字符。
precision和scale
precision属性和scale属性表示精度,当字段类型为double时,precision表示数值的总长度,scale表示小数点所占的位数。
(3)
@Column可以标注在属性前或getter方法前;
Ⅰ.@Column标注在属性前(建议使用这一种方式)
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; /** * 一卡通消费记录表 * @author Qian * */ @Entity @Table(name = "pb_op_card_consume") public class CardConsume { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", length = 20, nullable = false) private int id; /** * 交易编号 */ @Column(name = "tradeNo" ,length = 50 , nullable = false) private String tradeNo; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTradeNo() { return tradeNo; } public void setTradeNo(String tradeNo) { this.tradeNo = tradeNo; } }
Ⅱ.@Column标注getter方法前
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; /** * 一卡通消费记录表 * @author Qian * */ @Entity @Table(name = "pb_op_card_consume") public class CardConsume { /** * 交易编号 */ private String tradeNo; @Column(name = "tradeNo" ,length = 50 , nullable = false) public String getTradeNo() { return tradeNo; } public void setTradeNo(String tradeNo) { this.tradeNo = tradeNo; } }
提示:JPA规范中并没有明确指定那种标注方法,只要两种标注方式任选其一都可以。
(4)示例(其中3、4不常用)
Example 1: 指定字段“tradeNo”交易编号的长度为50,且值不能为null
@Column(name = "tradeNo", length = 50, nullable = false) private String tradeNo;
Example 2: 指定字段“totalAmount”交易金额的精度(长度)为10,小数点位数为2位,且值不能为null
@Column(name = "totalAmount", precision = 10, scale = 2, nullable = false) private BigDecimal totalAmount;
Example 3: 字段“text”,指定建表时SQL语句 如“varchar(50) NOT NULL”
@Column(name = "text", columnDefinition = "varchar(50) not null")
private String text;
等同于SQL
CREATE TABLE [dbo].[my_test] (
[id] int NOT NULL IDENTITY(1,1) ,
[text] varchar(50) NOT NULL
)
columnDefinition,若不指定该属性,通常使用默认的类型建表,若此时需要自定义建表的类型时,可在该属性中设置。
Example 4: 字段值为只读的,不允许插入和修改。通常用于主键和外键
@Column(name = "id", insertable = false, updatable = false) private Integer id;