@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;

@Column注解及属性详解相关推荐

  1. @Column注解属性详解

    目录 导读 是什么? 步骤 总结 导读 Java 注解(Annotation)又称 Java 标注,是 JDK5.0 引入的一种注释机制.Java 语言中的类.方法.变量.参数和包等都可以被标注.和 ...

  2. spring MVC请求处理类注解属性详解

    spring MVC请求处理类注解属性详解

  3. java rollback_Spring中的@Transactional(rollbackFor = Exception.class)属性详解

    序言 今天我在写代码的时候,看到了.一个注解@Transactional(rollbackFor = Exception.class),今天就和大家分享一下,这个注解的用法: 异常 如下图所示,我们都 ...

  4. display属性详解及用法

    display属性详解 1 block.inline.inline-block 2 flow-root 3 table.inline-table 4 flex.inline-flex 5 none 6 ...

  5. MPAndroidChart—— pieChart 属性详解

    MPAndroidChart GitHub 地址:https://github.com/PhilJay/MPAndroidChart pieChart 常用属性详解: [java] view plai ...

  6. 弹性盒子(flexbox)布局属性详解

    弹性盒子布局属性详解 1 弹性盒子概述 2 基本概念 3 常用属性 3.1 布局方向flex-direction 3.2 环绕效果flex-wrap 3.3 水平对齐方式justify-content ...

  7. python中文读音ndarray-numpy中的ndarray方法和属性详解

    NumPy数组的维数称为秩(rank),一维数组的秩为1,二维数组的秩为2,以此类推.在NumPy中,每一个线性的数组称为是一个轴(axes),秩其实是描述轴的数量.比如说,二维数组相当于是一个一维数 ...

  8. 源代码下载 第六章 注解式控制器详解

    2019独角兽企业重金招聘Python工程师标准>>> 源代码请到附件中下载. 其他下载: 跟着开涛学SpringMVC 第一章源代码下载 第二章 Spring MVC入门 源代码下 ...

  9. Meta http-equiv属性详解

    Meta http-equiv属性详解 博客分类: Web综合 HTML浏览器IECache搜索引擎  http-equiv顾名思义,相当于http的文件头作用,它可以向浏览器传回一些有用的信息,以帮 ...

最新文章

  1. Python装饰器实现一个代码计时器?
  2. Ubuntu中Vim使用技巧
  3. 检查是否有负循环(Bellman Ford, Floyd-Warshall)
  4. linux上的ds命令,Linux--容器命令
  5. 坑爹的SQL ISNUMERIC
  6. ORACLE TEXT DATASTORE PREFERENCE(四)
  7. 洛谷 2016 战略游戏(树形DP)
  8. Linux LAMP搭建
  9. [20160704]Block recover using RMAN.txt
  10. 自定义vue中的验证码组件
  11. 去除 火狐浏览器自动给域名前加 www.
  12. 回归分析常数项t值没有显著异于零怎么办_线性回归分析思路总结!简单易懂又全面!...
  13. 矛与盾:黑客攻防命令大曝光
  14. 【题解】Luogu P3110 [USACO14DEC]驮运Piggy Back
  15. Two‘s Complement(二进制补码)
  16. 关于Cause: java.lang.ClassNotFoundException: Cannot find class: 0(提示找不到类)报错
  17. nvm安装、下载以及nvm使用教程
  18. 工信部总工程师:建设网络强国振兴实体经济
  19. 南京大学声学基础(第三版)杜功焕第二章(未完结)
  20. 哪吒汽车的技术发布会都发布了什么?纯干货抢先看

热门文章

  1. 设计原则与思想:面向对象11讲
  2. java旅游_Java实现旅行商最短距离
  3. java中replaceall用法_java中replaceall的用法
  4. unity的模型动画
  5. 前端Js WebSocket测试代码,连websocket.org
  6. 基于PHP的互联网送水平台
  7. 腾讯面试题:两个玻璃杯判断哪个楼层恰巧碎裂
  8. 计算机网络(学习笔记)
  9. WiFi、WLAN的区别以及深入探索理解802.11协议
  10. java 删除字符串左边空格和右边空格 trimLeft trimRight