我看到一个例子,人们建议使用下面的
<property name="lastActivityDate" type="timestamp" >
<column name="C2810_LAST_ACTIVITY_DATE" />
<formula>select sysdate from dual</formula>
</property>
当我有公式标签时,我的 war 文件不会部署,它说属性映射的列数错误。但如果我删除它,一切都会正常。
我需要一种方法来获取数据库系统日期列。谁能帮帮我吗?
最佳答案
经过长时间的研发,终于找到了在日期列中使用hibernate插入/更新数据库SYSDATE(不是应用程序服务器日期)的解决方案。希望对您有帮助,请找到上述问题的解决方案
1)在mapping.hbm.xml文件中添加额外的属性,如下所示
property name="systemDate" formula="(select sysdate from dual)"
2) 为 POJO 类中的 'systemDate' 属性添加 setter 和 getter
private Date systemDate; public Date getSystemDate() {
return this.systemDate;
}
public void setSystemDate(Date systemDate) {
this.systemDate = systemDate;
}
3) 在 DAO 中,在插入或更新日期列之前,获取 systemDate 属性并使用该值保存 session 。
POJOclass Obj = new // the below line returns the database system date,because we had given the formula for this property in mapping.xml file
Date systemDate= POJOclassObj.getSystemDate(); and add the above Date to your columns. It will insert Database sysdate.
关于java - 如何在 hibernate 中使用数据库系统日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6696110/