我在我的應用程序中使用hibernate查詢MySQL數據庫的數據。現在我試圖查詢一個SQL Server,並且發現了有關日期類型的這個問題。休眠和SQL Server:時間戳格式必須是yyyy-mm-dd hh:mm:ss
在SQL服務器表Foo
有一個類型的列created_at
datetime
的相關POJO FOO有型java.util.Date
@Entity
@Table(name="foo")
public class Foo {
....
@Column(name="created_at")
private Date createdAt;
.....
}
的領域createdAt
但是,如果我執行HQL,如:
Query query = entityManager.createQuery("select f from Foo f");
query.getResultList();
我收到以下例外情況:
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Error al convertir el valor nvarchar al tipo de datos JDBC TIMESTAMP.
at com.microsoft.sqlserver.jdbc.DDC.convertStreamToObject(DDC.java:456)
at com.microsoft.sqlserver.jdbc.ServerDTVImpl.getValue(dtv.java:2007)
at com.microsoft.sqlserver.jdbc.DTV.getValue(dtv.java:175)
at com.microsoft.sqlserver.jdbc.Column.getValue(Column.java:113)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getValue(SQLServerResultSet.java:1982)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getValue(SQLServerResultSet.java:1967)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getTimestamp(SQLServerResultSet.java:2377)
at org.hibernate.type.descriptor.sql.TimestampTypeDescriptor$2.doExtract(TimestampTypeDescriptor.java:75)
at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:64)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:267)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:263)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253)
at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:338)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2969)
at org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl.loadFromResultSet(EntityReferenceInitializerImpl.java:324)
... 78 more
Caused by: java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]
at java.sql.Timestamp.valueOf(Timestamp.java:202)
at com.microsoft.sqlserver.jdbc.DDC.convertStringToObject(DDC.java:306)
at com.microsoft.sqlserver.jdbc.DDC.convertStreamToObject(DDC.java:419)
... 92 more
我該如何解決這個問題,我的後端代碼可以同時用於MySQL和MS SQL Server?
嘗試添加此註釋到您的字段:@Temporal(TemporalType.TIMESTAMP) –
它可能有助於http://stackoverflow.com/questions/7966291/how-can-i-map-a-java-date-to- datetime-in-mysql-by-default-its-timestamp-with -h –