| I've reproduced bug for Mongo and Neo4j and fixed it for Mongo. But I've noticed that bug may be in ORM core. If you look at org.hibernate.type.descriptor.java.JdbcTimestampTypeDescriptor#wrap (hibernate-core-5.2.13.Final.jar)
public <X> Date wrap(X value, WrapperOptions options) {
if (value == null) {
return null;
} else if (Timestamp.class.isInstance(value)) {
return (Timestamp)value;
} else if (Long.class.isInstance(value)) {
return new Timestamp((Long)value);
} else if (Calendar.class.isInstance(value)) {
return new Timestamp(((Calendar)value).getTimeInMillis());
} else if (Date.class.isInstance(value)) {
return (Date)value; } else {
throw this.unknownWrap(value.getClass());
}
}
But if I think right, this method should return Timestamp object or it's successor. But in case of input value be Date object, the result would be also Date object. And then it couse exception when assigning it to Timestamp field with exception:
org.hibernate.PropertyAccessException: Could not set field value [Thu Feb 15 13:28:08 UTC 2018] value by reflection : [class org.hibernate.ogm.datastore.mongodb.test.mapping.TimestampTestEntity.creationDateAndTime] setter of org.hibernate.ogm.datastore.mongodb.t
est.mapping.TimestampTestEntity.creationDateAndTime
at org.hibernate.ogm.datastore.mongodb.test.mapping.BuiltinTimestampTypeMappingTest.saveAndGet(BuiltinTimestampTypeMappingTest.java:161)
at org.hibernate.ogm.datastore.mongodb.test.mapping.BuiltinTimestampTypeMappingTest.testTimestampPersistedAsDateSupport(BuiltinTimestampTypeMappingTest.java:63)
Caused by: java.lang.IllegalArgumentException: Can not set java.sql.Timestamp field org.hibernate.ogm.datastore.mongodb.test.mapping.TimestampTestEntity.creationDateAndTime to java.util.Date
at org.hibernate.ogm.datastore.mongodb.test.mapping.BuiltinTimestampTypeMappingTest.saveAndGet(BuiltinTimestampTypeMappingTest.java:161)
at org.hibernate.ogm.datastore.mongodb.test.mapping.BuiltinTimestampTypeMappingTest.testTimestampPersistedAsDateSupport(BuiltinTimestampTypeMappingTest.java:63)
I propose to replace this with:
public <X> Date wrap(X value, WrapperOptions options) {
if (value == null) {
return null;
} else if (Timestamp.class.isInstance(value)) {
return (Timestamp)value;
} else if (Long.class.isInstance(value)) {
return new Timestamp((Long)value);
} else if (Calendar.class.isInstance( value)) {
return new Timestamp(((Calendar)value).getTimeInMillis());
} else if (Date.class.isInstance(value)) {
return new Timestamp( value.getTime() ); } else {
throw this.unknownWrap(value.getClass());
}
}
|