ColumnDefinition usage of Column annotation
columnDefinition attribute indicates that when creating a table, the SQL statement created by this field is generally used when generating table definitions through Entity. If the table in the database has been built, this attribute is not necessary to use it.
1. Specify the field type, length, whether null is allowed, whether unique, and default value
/** Warehouse number */ @Column(name = "code",columnDefinition = "Varchar(100) not null default'' unique") private String code;
2. Require special specified field type
@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;
Interpretation of various fields annotated by @Column
View source code
@Target({, }) @Retention() 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; }
explain
-
name
: Defines the name of the corresponding field to which the marked field is defined in the database table; -
unique
: Indicates whether the field is a unique identifier, defaults to false. If there is a field in the table that requires a unique identification, you can use either the tag or the @Table tag -
nullable
: Indicates whether the field can be null value, default is 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. The insertable and updatable attributes are generally used for read-only attributes, such as primary keys and foreign keys. The values of these fields are usually generated automatically. -
columnDefinition
(In most cases, it is almost unnecessary): means that when creating a table, the SQL statement created by this field is generally used when generating table definitions through Entity. (That is, if the table in DB has been built, this property is not necessary. -
table
: Indicates that when multiple tables are mapped, the fields in the table specified are specified. The default value is the table name of the main table. -
length
: represents the length of the field. This property is only valid when the field type is varchar, and the default is 255 characters. -
precision
andscale
:precision attribute and scale attribute represent precision. When the field type is double, precision indicates the total length of the value, and scale indicates the number of digits occupied by the decimal point.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.