如何用hibernate生成记录修改和建立的时间戳
1个回答
展开全部
看看下面这段说明
/** USE [Temporal] annotation to mapping Date(Java) to TIMESTAMP(DB),
* USE [Generated] means hibernate will not take responsibility for generating its value. You should choose trigger to generate this value in DB side.
* Hibernate will select this value from database after insert or update, so that the in-memory entity has proper value.*/
@Column(name="CREATE_DATE", nullable=true, updatable=false)
@Temporal(TemporalType.TIMESTAMP)
@Generated(GenerationTime.INSERT)
private Date createDate;
@Column(name="LAST_UPDATED", nullable=true)
@Temporal(TemporalType.TIMESTAMP)
@Generated(GenerationTime.ALWAYS)
private Date lastUpdated;
本以为用了Generated能自动生成建立和修改时间,想想Grenerated这个是个过去时,什么意思就是已经生成了,由谁生成呢?当然是在数据库中用触发器或是其他数据库方法或是自己直接写在程序里生成的.找了半天本以为时间可以自己生成的.不过可以这样写
private Date createDate=new Date();
private Date lastUpdated=new Date();
这样不是就自己生成了。
/** USE [Temporal] annotation to mapping Date(Java) to TIMESTAMP(DB),
* USE [Generated] means hibernate will not take responsibility for generating its value. You should choose trigger to generate this value in DB side.
* Hibernate will select this value from database after insert or update, so that the in-memory entity has proper value.*/
@Column(name="CREATE_DATE", nullable=true, updatable=false)
@Temporal(TemporalType.TIMESTAMP)
@Generated(GenerationTime.INSERT)
private Date createDate;
@Column(name="LAST_UPDATED", nullable=true)
@Temporal(TemporalType.TIMESTAMP)
@Generated(GenerationTime.ALWAYS)
private Date lastUpdated;
本以为用了Generated能自动生成建立和修改时间,想想Grenerated这个是个过去时,什么意思就是已经生成了,由谁生成呢?当然是在数据库中用触发器或是其他数据库方法或是自己直接写在程序里生成的.找了半天本以为时间可以自己生成的.不过可以这样写
private Date createDate=new Date();
private Date lastUpdated=new Date();
这样不是就自己生成了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询