Hibernate结合SQlSever2012做查询操作,在出生日期发生的类型转换错误:
以下是数据库设计:
如图birthday数据类型为date;
以下是报错信息:
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 将 nvarchar 值转换为 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:67)
at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:64)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:261)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:257)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:247)
at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:332)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2912)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1673)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1605)
at org.hibernate.loader.Loader.getRow(Loader.java:1505)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:713)
at org.hibernate.loader.Loader.processResultSet(Loader.java:943)
at org.hibernate.loader.Loader.doQuery(Loader.java:911)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:312)
at org.hibernate.loader.Loader.loadEntity(Loader.java:2121)
… 14 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)
… 36 more
尝试做法:
// //先将date类型转换成String类型
// DateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
// String birthday = sdf.format(admin.getBirthday());
// //String类型转换成TimeStamp
// Timestamp ts = new Timestamp(System.currentTimeMillis());
// ts = Timestamp.valueOf(birthday);
但是还是报错:空指针异常。。。
解决办法:
直接把数据库的date类型转为datetime;