Usage of columnDefinition based on Column annotation

  • 2021-12-04 10:04:47
  • OfStack

Directory Column annotation columnDefinition uses 1, specifies the field type, length, whether null is allowed, whether only 1. Default value 2, needs to specify the special field type situation @ Column annotation each field explanation view source code explanation

columnDefinition Use of Column Annotations

The columnDefinition attribute indicates the SQL statement created by this field when creating a table, which is generally used when generating a table definition through Entity. If the table in the database has been built, this attribute is unnecessary

1. Specify the field type, length, whether null is allowed or not, and whether it is only 1. Default value


/**  Warehouse number  */
@Column(name = "code",columnDefinition = "Varchar(100) not null default'' unique")
private String code;

2. The case where a special field type needs to be specified


@Column(name = "remark",columnDefinition="text")
private String remark;

@Column(name = "salary", columnDefinition = "decimal(5,2)")
private BigDecimal salary;

@Column(name="birthday",columnDefinition="date")
private Date birthday;
@Column(name="createTime",columnDefinition="datetime")
private Date createTime;

Explanation of each field of @ Column annotation

View the source code


@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.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;
}

Explanation

name Defines the name of the field corresponding to the marked field in the database table; unique Indicates whether this field is a 1-only identity, and defaults to false. If there is a 1 field in the table that requires a 1-only identity, you can use either this tag or the in the @ Table tag nullable Indicates whether the field can be an null value, which defaults to true insertable Indicates whether the value of this field needs to be inserted when inserting data using the "INSERT" script. updatable Indicates whether the value of this field needs to be updated when inserting data using the "UPDATE" script. insertable and updatable attribute 1 are used for read-only attributes such as primary and foreign keys. The values of these fields are usually generated automatically. columnDefinition (Most of the time, hardly used): Represents the SQL statement created by this field when a table is created, 1 generally used when generating a table definition from Entity. That is, if the table in DB is already built, this property is not necessary. table Represents the fields in the table of the specified table when mapping multiple tables. The default value is the table name of the primary table. length Represents the length of the field, which is valid when the field is of type varchar and defaults to 255 characters. precision And scale The precision and scale attributes represent precision, precision represents the total length of the value when the field type is double, and scale represents the number of decimal places

Related articles: