问题
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.
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
}
回答1:
Try registering a new Driver like this:
public class DateTime2SQLServerDialect extends SQLServer2008Dialect {
public DateTime2SQLServerDialect () {
super();
registerColumnType(Types.DATE, "datetime2");
}
}
and then use this dialect instead:
<property name="hibernate.dialect">my.package.DateTime2SQLServerDialect</property>
Also try changing this:
<return-scalar column="completionTime" type="date" />
to this:
<return-scalar column="completionTime" type="timestamp" />
回答2:
If you're using entities you can specify the column type as such:
@Column(columnDefinition="datetime2")
private Date completionTime;
回答3:
I am able to solve above scenario by changing datatype as below,driver registrayion is not required:
<hibernate-mapping>
<sql-query name="getMLC">
<return-scalar column="mlcid" type="int" />
<return-scalar column="completionTime" type="timestamp" />
{ call lsc.MLC_Get(:ABC, :XYZ, :ErrorCode)}
</sql-query>
</hibernate-mapping>
class Mlc implements java.io.Serializable {
private Integer mlcid;
private TimeStamp completionTime;
// getter and setter
}
来源:https://stackoverflow.com/questions/30028345/hibernate-mssql-datetime2-mapping