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的方法

转载于:https://my.oschina.net/u/2489258/blog/1807099

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值