hibernate设置时间戳的默认值和更新时间的自动更新

  1. 在须要建立时间、更新时间这两个字段的数据表执行如下sql
alter table user add create_time timestamp not null default current_timestamp comment '建立时间';
   alter table user add update_time timestamp not null default current_timestamp on update current_timestamp comment '修改时间';
  1. 对应的实体增长这个两个属性,而且标上对应的注解。在测试中发现,若是不执行第一步操做,使用hibernate自动建立的表和字段,没法自动填充建立时间和更新时间,也就是说插入数据时这两个属性都是空的。
@Temporal(TemporalType.TIMESTAMP)  
    @Column(name="create_time",  nullable=true, updatable = false)  
    @Generated(GenerationTime.INSERT)
	private Date createTime;
    @Temporal(TemporalType.TIMESTAMP)  
    @Column(name="update_time",  nullable=true)  
    @Generated(GenerationTime.ALWAYS)
	private Date updateTime;
    // 省略其它属性和get、set的方法
相关文章
相关标签/搜索