Hibernate MSSQL datetime2映射 [英] Hibernate MSSQL datetime2 mapping
问题描述
我有一个存储过程,该存储过程返回数据库中数据类型为datetime2以及Java文件中Date为数据的列。当我尝试在从数据库获取的时间上调用getTime()时。返回的时间为19994321211毫秒,相当于IST 2015年5月4日星期一。理想情况下,它应返回的时间为2015-05-04 15:21:52的毫秒数,因为这是直接执行过程时数据库中显示的时间。
I have a stored procedure which is returning a column having datatype as datetime2 in database and Date in Java file. When I am trying to call getTime() on that obtained time from database. It's returning 19994321211 millisecond which is equivalent to Mon May 04 00:00:00 IST 2015. ideally it should return milliseconds of 2015-05-04 15:21:52 as this is the time shown in database when procedure is executed directly.
由于我是Hibernate的新手,所以我无法理解该问题是否与hibernate映射有关,或者我错过了其他事项。
As I am new to Hibernate , I am unable to understand if the issue is related to hibernate mapping or something else I am missing out.
<hibernate-mapping>
<sql-query name="getMLC">
<return-scalar column="mlcid" type="int" />
<return-scalar column="completionTime" type="date" />
{ call lsc.MLC_Get(:ABC, :XYZ, :ErrorCode)}
</sql-query>
</hibernate-mapping>
class Mlc implements java.io.Serializable {
private Integer mlcid;
private Date completionTime;
// getter and setter
}
推荐答案
尝试像这样注册新的驱动程序:
Try registering a new Driver like this:
public class DateTime2SQLServerDialect extends SQLServer2008Dialect {
public DateTime2SQLServerDialect () {
super();
registerColumnType(Types.DATE, "datetime2");
}
}
,然后改用此方言:
<property name="hibernate.dialect">my.package.DateTime2SQLServerDialect</property>
也可以尝试更改此内容:
Also try changing this:
<return-scalar column="completionTime" type="date" />
为此:
<return-scalar column="completionTime" type="timestamp" />
这篇关于Hibernate MSSQL datetime2映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!