@Column
@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标记时,需要注意以下几个问题:
l 此标记可以标注在getter方法或属性前,例如以下的两种标注方法都是正确的:
标注在属性前:
@Entity
@Table(name = "contact")
public class ContactEO{
@Column(name=" contact_name ")
private String name;
}
标注在getter方法前:
@Entity
@Table(name = "contact")
public class ContactEO{
@Column(name=" contact_name ")
public String getName() {
return name;
}
}
l unique属性表示该字段是否为唯一标识,默认为false。如果表中有一个字段需要唯一标识,则既可以使用该标记,也可以使用@Table标记中的@UniqueConstraint。
l nullable属性表示该字段是否可以为null值,默认为true。
l insertable属性表示在使用“INSERT”脚本插入数据时,是否需要插入该字段的值。
l updatable属性表示在使用“UPDATE”脚本插入数据时,是否需要更新该字段的值。insertable和updatable属性一般多用于只读的属性,例如主键和外键等。这些字段的值通常是自动生成的。
l columnDefinition属性表示创建表时,该字段创建的SQL语句,一般用于通过Entity生成表定义时使用。
l table属性表示当映射多个表时,指定表的表中的字段。默认值为主表的表名。有关多个表的映射将在本章的5.6小节中详细讲述。
l length属性表示字段的长度,当字段的类型为varchar时,该属性才有效,默认为255个字符。
l precision属性和scale属性表示精度,当字段类型为double时,precision表示数值的总长度,scale表示小数点所占的位数。
下面举几个小例子:
示例一:指定字段“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属性设置的值。若不指定该属性,通常使用默认的类型建表,若此时需要自定义建表的类型时,可在该属性中设置。
示例四:字段值为只读的,不允许插入和修改。通常用于主键和外键。
private Integer id;
@Column(name="id",insertable=false,updatable=false)
public Integer getId() {
return id;
}
@Column的更多相关文章
- 解决 Springboot Unable to build Hibernate SessionFactory @Column命名不起作用
问题: Springboot启动报错: Caused by: org.springframework.beans.factory.BeanCreationException: Error creati ...
- [LeetCode] Excel Sheet Column Number 求Excel表列序号
Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, retur ...
- [LeetCode] Excel Sheet Column Title 求Excel表列名称
Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...
- com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'dd' in 'where clause'
今天在使用mysql数据库查找数据的时候报错,错误信息如下: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown co ...
- 理解Compressed Sparse Column Format (CSC)
最近在看<Spark for Data Science>这本书,阅读到<Machine Learning>这一节的时候被稀疏矩阵的存储格式CSC给弄的晕头转向的.所以专门写一篇 ...
- Reset Identity Column Value in SQL Server (Identity Reset)
前言:今天在群里看到有人在问SQL Server自增值重置问题(sqlserver identiy column value reset ) 闲话少说,直接上代码: 正文: --create tabl ...
- NPOI导出Excel (C#) 踩坑 之--The maximum column width for an individual cell is 255 charaters
/******************************************************************* * 版权所有: * 类 名 称:ExcelHelper * 作 ...
- ResultSet can not re-read row data for column 1.
error:ResultSet can not re-read row data for column 1. 将数据类型改为varchar(max)后,查询数据错误 改正:将jdbc驱动改为jtds驱 ...
- Ext.js细节:在MVC中处理Widget Column,GetCmp和ComponentQuery, Id和ItemId
针对EXT.JS版本的演进,要不断的学习新的最佳实践方法. 比如,在定义组件时,尽管用itemid,而不是id. 在搜索组件时,尽量用ComponentQuery,而不是getCmp. 在MVC中处理 ...
随机推荐
- Oracle连接odbc数据源
Oracle连接odbc数据源 说明 oracle连接ODBC数据源有两种方式,hsodbc和dg4odbc,简单说dg4odbc是hsodbc的升级.两种连接方法大致一样,现将连接步骤说明如下: 检 ...
- auto_clipboard
黄山松发表于博客园:http://www.cnblogs.com/tomview/p/6137179.html #ifndef __HSS_AUTO_CLIPBOARD_HSS__#define __ ...
- 怎么实现ZBrush 4R7中界面视窗的快速转换
本篇教程介绍ZBrush® 4R7界面的基本操作之转换界面视窗, 教程属于入门教程可以帮助新手快速入门.因为ZBrush工 作界面不同于其他我们所熟知的3D软件,初次接触ZBrush的时候难免会有所困 ...
- [C]指针有什么好处?
指针的好处,需要和数组比较起来说.具体如下: 1.指针可以随意申请不连续的数据存储空间,而数组是连续的,如果数组空间没有全部占用,那么会造成浪费,比如你申请了a[10],缺只有5个数据输入,那 ...
- ngx_http_proxy_module模块.md
ngx_http_proxy_module ngx_http_proxy_module模块允许将请求传递到另一个服务器. proxy_bind Syntax: proxy_bind address [ ...
- mysql timeout知多少
1.timeout变量知多少 打开mysql,用show variables like '%timeout%'命令一看,不看不知道,一看吓一跳,结果如下面所示,这么多timeout相关变量,一下就吓尿 ...
- Hemodynamic response function (HRF) - FAQ
Source: MIT - Mindhive What is the 'canonical' HRF? The very simplest design matrix for a given expe ...
- Linux Posix线程条件变量
生产者消费者模型 .多个线程操作全局变量n,需要做成临界区(要加锁--线程锁或者信号量) .调用函数pthread_cond_wait(&g_cond,&g_mutex)让这个线程锁在 ...
- [LeetCode] Android Unlock Patterns 安卓解锁模式
Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...