zoukankan      html  css  js  c++  java
  • @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;

    }

    }

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

    l         unique属性表示该字段是否为唯一标识,默认为false。如果表中有一个字段需要唯一标识,则既可以使用该标记,也可以使用@Table标记中的@UniqueConstraint

    l         nullable属性表示该字段是否可以为null值,默认为true

    l         insertable属性表示在使用“INSERT”脚本插入数据时,是否需要插入该字段的值。

    l         updatable属性表示在使用“UPDATE”脚本插入数据时,是否需要更新该字段的值。insertableupdatable属性一般多用于只读的属性,例如主键和外键等。这些字段的值通常是自动生成的。

    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属性设置的值。若不指定该属性,通常使用默认的类型建表,若此时需要自定义建表的类型时,可在该属性中设置。

    提示:通过Entity定义生成表,还是通过表配置Entity,这两种ORM的策略。有关两种方法的映射策略好坏,将在本书的章节中“JPA工具的使用”一章进行详细的比较。

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

             private Integer id;

            

             @Column(name="id",insertable=false,updatable=false)

             public Integer getId() {

                       return id;

             }

  • 相关阅读:
    Oracle 查看表空间的大小及使用情况sql语句
    Oracle审计--AUD$占用空间较大处理方案
    system表空间爆满解决方法
    Oracle查询库中记录数大于2千万的所有表
    oracle 百万行数据优化查询
    React (Native) Rendering Lifecycle
    React于React native的渲染机制
    Virtual DOM的渲染机制--猜测
    react的优点:兼容了dsl语法与UI的组件化管理
    What is Babel?---JSX and React
  • 原文地址:https://www.cnblogs.com/jasontec/p/9601724.html
Copyright © 2011-2022 走看看