JobPlus知识库 IT 大数据 文章
@Column注解及属性详解

@Column注解

用来标识实体类中属性与数据表中字段的对应关系

(1)源码:

[html] 

  1. /*  
  2.  * Copyright (c) 2008, 2009, 2011 Oracle, Inc. All rights reserved.  
  3.  *  
  4.  * This program and the accompanying materials are made available under the  
  5.  * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0  
  6.  * which accompanies this distribution.  The Eclipse Public License is available  
  7.  * at http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution License  
  8.  * is available at http://www.eclipse.org/org/documents/edl-v10.php.  
  9.  */  
  10. package javax.persistence;  
  11.   
  12. import java.lang.annotation.Retention;  
  13. import java.lang.annotation.Target;  
  14.   
  15. import static java.lang.annotation.ElementType.FIELD;  
  16. import static java.lang.annotation.ElementType.METHOD;  
  17. import static java.lang.annotation.RetentionPolicy.RUNTIME;  
  18.   
  19. /**  
  20.  * Is used to specify the mapped column for a persistent property or field.  
  21.  * If no <code>Column</code> annotation is specified, the default values apply.  
  22.  *  
  23.  * <blockquote><pre>  
  24.  *    Example 1:  
  25.  *  
  26.  *    @Column(name="DESC", nullable=false, length=512)  
  27.  *    public String getDescription() { return description; }  
  28.  *  
  29.  *    Example 2:  
  30.  *  
  31.  *    @Column(name="DESC",  
  32.  *            columnDefinition="CLOB NOT NULL",  
  33.  *            table="EMP_DETAIL")  
  34.  *    @Lob  
  35.  *    public String getDescription() { return description; }  
  36.  *  
  37.  *    Example 3:  
  38.  *  
  39.  *    @Column(name="ORDER_COST", updatable=false, precision=12, scale=2)  
  40.  *    public BigDecimal getCost() { return cost; }  
  41.  *  
  42.  * </pre></blockquote>  
  43.  *  
  44.  *  
  45.  * @since Java Persistence 1.0  
  46.  */  
  47. @Target({METHOD, FIELD})  
  48. @Retention(RUNTIME)  
  49. public @interface Column {  
  50.   
  51.     /**  
  52.      * (Optional) The name of the column. Defaults to  
  53.      * the property or field name.  
  54.      */  
  55.     String name() default "";  
  56.   
  57.     /**  
  58.      * (Optional) Whether the column is a unique key.  This is a  
  59.      * shortcut for the <code>UniqueConstraint</code> annotation at the table  
  60.      * level and is useful for when the unique key constraint  
  61.      * corresponds to only a single column. This constraint applies  
  62.      * in addition to any constraint entailed by primary key mapping and  
  63.      * to constraints specified at the table level.  
  64.      */  
  65.     boolean unique() default false;  
  66.   
  67.     /**  
  68.      * (Optional) Whether the database column is nullable.  
  69.      */  
  70.     boolean nullable() default true;  
  71.   
  72.     /**  
  73.      * (Optional) Whether the column is included in SQL INSERT  
  74.      * statements generated by the persistence provider.  
  75.      */  
  76.     boolean insertable() default true;  
  77.   
  78.     /**  
  79.      * (Optional) Whether the column is included in SQL UPDATE  
  80.      * statements generated by the persistence provider.  
  81.      */  
  82.     boolean updatable() default true;  
  83.   
  84.     /**  
  85.      * (Optional) The SQL fragment that is used when  
  86.      * generating the DDL for the column.  
  87.      * <p> Defaults to the generated SQL to create a  
  88.      * column of the inferred type.  
  89.      */  
  90.     String columnDefinition() default "";  
  91.   
  92.     /**  
  93.      * (Optional) The name of the table that contains the column.  
  94.      * If absent the column is assumed to be in the primary table.  
  95.      */  
  96.     String table() default "";  
  97.   
  98.     /**  
  99.      * (Optional) The column length. (Applies only if a  
  100.      * string-valued column is used.)  
  101.      */  
  102.     int length() default 255;  
  103.   
  104.     /**  
  105.      * (Optional) The precision for a decimal (exact numeric)  
  106.      * column. (Applies only if a decimal column is used.)  
  107.      * Value must be set by developer if used when generating  
  108.      * the DDL for the column.  
  109.      */  
  110.     int precision() default 0;  
  111.   
  112.     /**  
  113.      * (Optional) The scale for a decimal (exact numeric) column.  
  114.      * (Applies only if a decimal column is used.)  
  115.      */  
  116.     int scale() default 0;  
  117. }  

(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标注在属性前(建议使用这一种方式)

[html] 

  1. import javax.persistence.Column;  
  2. import javax.persistence.Entity;  
  3. import javax.persistence.GeneratedValue;  
  4. import javax.persistence.GenerationType;  
  5. import javax.persistence.Id;  
  6. import javax.persistence.Table;  
  7.   
  8. /**  
  9.  * 一卡通消费记录表  
  10.  * @author Qian  
  11.  *  
  12.  */  
  13. @Entity  
  14. @Table(name = "pb_op_card_consume")  
  15. public class CardConsume {  
  16.   
  17.     @Id  
  18.     @GeneratedValue(strategy = GenerationType.IDENTITY)  
  19.     @Column(name = "id", length = 20, nullable = false)  
  20.     private int id;  
  21.   
  22.     /**  
  23.      * 交易编号  
  24.      */  
  25.     @Column(name = "tradeNo" ,length = 50 , nullable = false)  
  26.     private String tradeNo;  
  27.   
  28.     public int getId() {  
  29.         return id;  
  30.     }  
  31.   
  32.     public void setId(int id) {  
  33.         this.id = id;  
  34.     }  
  35.   
  36.     public String getTradeNo() {  
  37.         return tradeNo;  
  38.     }  
  39.   
  40.     public void setTradeNo(String tradeNo) {  
  41.         this.tradeNo = tradeNo;  
  42.     }  
  43.       
  44. }  

Ⅱ.@Column标注getter方法前

[html] 

  1. import javax.persistence.Column;  
  2. import javax.persistence.Entity;  
  3. import javax.persistence.Table;  
  4.   
  5. /**  
  6.  * 一卡通消费记录表  
  7.  * @author Qian  
  8.  *  
  9.  */  
  10. @Entity  
  11. @Table(name = "pb_op_card_consume")  
  12. public class CardConsume {  
  13.   
  14.     /**  
  15.      * 交易编号  
  16.      */  
  17.     private String tradeNo;  
  18.   
  19.     @Column(name = "tradeNo" ,length = 50 , nullable = false)  
  20.     public String getTradeNo() {  
  21.         return tradeNo;  
  22.     }  
  23.   
  24.     public void setTradeNo(String tradeNo) {  
  25.         this.tradeNo = tradeNo;  
  26.     }  
  27.       
  28. }  

提示:JPA规范中并没有明确指定那种标注方法,只要两种标注方式任选其一都可以。

(4)示例(其中3、4不常用)

Example 1:    指定字段“tradeNo”交易编号的长度为50,且值不能为null

[html] 

  1. @Column(name = "tradeNo", length = 50, nullable = false)  
  2. private String tradeNo;  

Example 2:    指定字段“totalAmount”交易金额的精度(长度)为10,小数点位数为2位,且值不能为null

[html] 

  1. @Column(name = "totalAmount", precision = 10, scale = 2, nullable = false)  
  2. private BigDecimal totalAmount;  

Example 3:    字段“text”,指定建表时SQL语句 如“varchar(50) NOT NULL”

[html]

  1. @Column(name = "text", columnDefinition = "<span style="color:#ff0000;"><strong>varchar(50) not null</strong></span>")  
  2. private String text;  

等同于SQL

[html] 

  1. CREATE TABLE [dbo].[my_test] (  
  2.     [id] int NOT NULL IDENTITY(1,1) ,  
  3.     <span style="background-color:rgb(255,0,0);">[text] varchar(50) NOT NULL </span>  
  4. )  

columnDefinition,若不指定该属性,通常使用默认的类型建表,若此时需要自定义建表的类型时,可在该属性中设置。

Example 4:    字段值为只读的,不允许插入和修改。通常用于主键和外键

[html] 

  1. @Column(name = "id", insertable = false, updatable = false)  
  2. private Integer id;  


如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

¥ 打赏支持
191人赞 举报
分享到
用户评价(0)

暂无评价,你也可以发布评价哦:)

12 人收藏了这篇文章
腾讯云数据库性能卓越稳定可靠,为您解决数据库运维难题
广告
扫码APP

扫描使用APP

扫码使用

扫描使用小程序