public @interface Column {
/**
* (Optional) The name of the column. Defaults to
* the property or field name.
*/
String name() default "";
/**
* (Optional) Whether the column is a unique key. This is a
* shortcut for the <code>UniqueConstraint</code> annotation at the table
* level and is useful for when the unique key constraint
* corresponds to only a single column. This constraint applies
* in addition to any constraint entailed by primary key mapping and
* to constraints specified at the table level.
*/
boolean unique() default false;
/**
* (Optional) Whether the database column is nullable.
*/
boolean nullable() default true;
/**
* (Optional) Whether the column is included in SQL INSERT
* statements generated by the persistence provider.
*/
boolean insertable() default true;
/**
* (Optional) Whether the column is included in SQL UPDATE
* statements generated by the persistence provider.
*/
boolean updatable() default true;
/**
* (Optional) The SQL fragment that is used when
* generating the DDL for the column.
* <p> Defaults to the generated SQL to create a
* column of the inferred type.
*/
String columnDefinition() default "";
/**
* (Optional) The name of the table that contains the column.
* If absent the column is assumed to be in the primary table.
*/
String table() default "";
/**
* (Optional) The column length. (Applies only if a
* string-valued column is used.)
*/
int length() default 255;
/**
* (Optional) The precision for a decimal (exact numeric)
* column. (Applies only if a decimal column is used.)
* Value must be set by developer if used when generating
* the DDL for the column.
*/
int precision() default 0;
/**
* (Optional) The scale for a decimal (exact numeric) column.
* (Applies only if a decimal column is used.)
*/
int scale() default 0;
}
两条sql语句
creat_time datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
update_time timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
即:
数据创建时间用 CURRENT_TIMESTAMP ---insert此条数据的时间(之后就不变了)
数据更新时间用 UPDATE CURRENT_TIMESTAMP---update此条数据的时间(数据字段有变更时,会更新为当前时间)
DEFAULT CURRENT_TIMESTAMP
insert的时候,在用户不给定值的情况下[ jpa @column(insertable=false) ],使用默认值;
注意:即使用户给定的值是null,还是会使用用户插入的值。
insertable为false的意思,就是生成insert语句的时候不包含这个字段,因此值未知了就使用默认值。
ON UPDATE CURRENT_TIMESTAMP
update的使用,在用户不给定值的情况下[ jpa @column(updateable=false) ],使用默认值;
注意:即使用户给定的值是null,还是会使用用户给定的值。
updateable为false的意思,就是生成update语句的时候不包含这个字段,因此值未知了就使用默认值。
MySQL时区问题
show variables like '%time_zone';

这里有两个time_zone:
system_time_zone: JVM 插入java.util.Date()时默认的time_zone.
time_zone: 又称为MySQL session time_zone,数据库使用timestamp等函数时默认的time_zone.
本文介绍JPA注解中@Column属性的作用及配置方法,并详细解析MySQL中使用CURRENT_TIMESTAMP作为默认值和更新值的场景。同时,讨论了不同情况下如何设置JPA的insertable和updatable属性。
9681

被折叠的 条评论
为什么被折叠?



