hibernate中创建时间、更新时间字段的配置

项目中,一般在数据库中会记录该条目的创建时间,以及后续该条目被更新的时间。创建时间即插入数据库记录的时间,之后条目被update时不会更新,更新时间则在每次update时都会更新。那么,在hibernate中怎么达到这一目的呢。

实测以下方法可行:
假设我有一个bean名为 imgFile,对应的数据库表为t_imgfile
其中create_time表示创建时间,对应bean的属性是createTime
update_time表示更新时间,对应bean的属性是updateTime

xx.hbm.xml中应如下设置
update  =  "false"表示在update时不更新, insert  =  "true"表示在插入时需要更新
[html]  view plain  copy
  1.         < property name = "createTime" update = "false" insert = "true" >    
  2.             < column name = "create_time" sql-type = "timestamp" default = "CURRENT_TIMESTAMP" />    
  3.         </ property>  
  4.        < property name = "updateTime" update = "true" insert = "true" >    
  5.             < column name = "update_time" sql-type = "timestamp" default = "CURRENT_TIMESTAMP" />    
  6.         </ property>  



插入数据库前,程序里需对bean进行赋值,将这两个值set为当前时间如下
[java]  view plain  copy
  1. imgFile.setCreateTime(new Timestamp(System.currentTimeMillis()));  
  2. imgFile.setUpdateTime(new Timestamp(System.currentTimeMillis()));  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值