就像@Table注解用来标识实体类与数据表的对应关系类似,@Column注解来标识实体类中属性与数据表中字段的对应关系。

该注解的定义如下:

@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注解一共有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表示小数点所占的位数。

API文档地址:http://docs.oracle.com/javaee/5/api/javax/persistence/Column.html

在使用此@Column标记时,需要注意以下几个问题:

此标记可以标注在getter方法或属性前,例如以下的两种标注方法都是正确的:

标注在属性前:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name = "contact")
public class ContactEO {

@Column(name = " contact_name ")
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

标注在getter方法前:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name = "contact")
public class ContactEO {
private String name;

@Column(name = " contact_name ")
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

提示:JPA规范中并没有明确指定那种标注方法,只要两种标注方式任选其一都可以。这根据个人的喜好来选择,笔者习惯使用第二种方法。

下面举几个小例子:

示例一:指定字段“contact_name”的长度是“512”,并且值不能为null。

private String name;
@Column(name="contact_name",nullable=false,length=512)

public String getName() {
return name;

}

创建的SQL语句如下所示。

CREATE TABLE contact (
id integer not null,
contact_name varchar (512) not null,
primary key (id)
)

示例二:指定字段“monthly_income”月收入的类型为double型,精度为12位,小数点位数为2位。

private BigDecimal monthlyIncome;
@Column(name="monthly_income",precision=12, scale=2)
public BigDecimal getMonthlyIncome() {
return monthlyIncome;
}

创建的SQL语句如下所示。

CREATE TABLE contact (
id integer not null,
monthly_income double(12,2),
primary key (id)
)

示例三:自定义生成CLOB类型字段的SQL语句。

private String name;
@Column(name=" contact_name ",columnDefinition="clob not null")
public String getName() {
return name;
}

生成表的定义SQL语句如下所示。

CREATE TABLE contact (
id integer not null,
contact_name clob (200) not null,
primary key (id)
)

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

提示:通过Entity定义生成表,还是通过表配置Entity,这是两种不同的ORM策略。

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

private Integer id;

@Column(name="id",insertable=false,updatable=false)
public Integer getId() {
return id;
}

JPA的Column注解总结的更多相关文章

  1. Spring Boot集成JPA的Column注解命名字段无效的问题

    偶然发现,Spring Boot集成jpa编写实体类的时候,默认使用的命名策略是下划线分隔的字段命名. Spring Boot版本:1.5.4.release 数据表: id int, userNam ...

  2. 如何解决jpa 要求column 名称单词必须用下划线

    [转]:http://www.jeesns.cn/article/detail/6657 先引出轮子http://blog.csdn.net/54powerman/article/details/76 ...

  3. JPA学习(2)注解

    上一篇学习了JPA的helloworld,也初略的使用了一些注解,接下来就细细的了解一下有哪些注解,和这些注解的作用 JPA的基本注解: ①@Entity,@Table,@Id,@GeneratedV ...

  4. 二十、springboot之jpa开发@MappedSuperclass 注解说明

    @MappedSuperclass使用条件: 当我们进行开发项目时,我们经常会用到实体映射到数据库表的操作,此时我们经常会发现在我们需要映射的几个实体类中,有几个共同的属性,例如编号ID,创建者,创建 ...

  5. SpringBoot+MyBatis中自动根据@Table注解和@Column注解生成增删改查逻辑

    习惯使用jpa操作对象的方式,现在用mybatis有点不习惯. 其实是懒得写SQL,增删改查那么简单的事情你帮我做了呗,mybatis:NO. 没办法,自己搞喽! 这里主要是实现了通过代码自动生成my ...

  6. JPA API与注解

    一.JPA API Persistence 类:用于获取 EntityManagerFactory 实例,该类含有静态方法 createEntityManagerFactory. //persiste ...

  7. 如何使用JPA的@Formula注解?

    背景描述 我们经常会在项目中用到一些数据字典,在存储和传输时使用Code,在前端展示时使用Name,这样做的好处是便于系统维护,比如项目中用到了"医院"这个名称,如果后期需求发生变 ...

  8. 利用Eclipse的JPA自动生成注解实体

    新公司用的SSH(springmvc)框架,看代码的时候,发现没有hbm.xml文件,全部使用的注解形式.在一次闲聊的时候问同事,这么多entity  写起来不麻烦么.同事说根据数据库自动生成的.于是 ...

  9. 使用JPA中@Query 注解实现update 操作

    spring使用jpa进行update操作主要有两种方式: 1.调用保存实体的方法 1)保存一个实体:repository.save(T entity) 2)保存多个实体:repository.sav ...

随机推荐

  1. 解决json跨域时错误:SyntaxError: invalid label

    将数据做以下返回: $callback = $_GET['callback']; echo $callback.'('.json_encode(array('html'=>$html)).')' ...

  2. A380上11万一张的机票什么享受?来看看

    上个月底,全球奢华航班排行榜出炉,新加坡航空头等舱荣登第一.不过,比头等舱更豪奢的,将近两万美元一张往返票的“套间”又是怎么样的呢? 新加坡航空的一名常旅客Derek Low就体验了一把全球最豪奢的坐 ...

  3. HTML--10Jquery

    在<网页制作Dreamweaver(悬浮动态分层导航)>中,运用到了jQuery的技术,轻松实现了菜单的下拉.显示.隐藏的效果,不必再用样式表一点点地修改,省去了很多麻烦,那么jQuery ...

  4. c++中的243、251、250错误原因

    c++中的243.251.250错误,原因可能是不小心输入了中文符号.

  5. Mysql5.0以下 手工注入

    order by 20 www. .com/product/introduction.php?id=-65 UNION SELECT user(),2 www. .com/product/introd ...

  6. C/C++整数除法以及保留小数位的问题

    题目描述 Given two postive integers A and B,  please calculate the maximum integer C that C*B≤A, and the ...

  7. Display Images in widget

    在自定义的widget中显示图片. 思路:定义类MyWidget,public 继承自QWidget,然后实现 void paintEvent(QPaintEvent *). 新建Empty qmak ...

  8. two Sum ---- LeetCode 001

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  9. sqlserver中自定义函数+存储过程实现批量删除

    由于项目的需要,很多模块都要实现批量删除的功能.为了方便模块的调用,把批量删除方法写成自定义函数.直接上代码. 自定义函数: ALTER FUNCTION [dbo].[func_SplitById] ...

  10. Nearest number - 2_暴力&&bfs

    Description Input is the matrix A of N by N non-negative integers. A distance between two elements A ...