如何在mongodb中保存java.sql.date对象?

时间:2015-11-19 10:16:31

标签: java spring mongodb spring-data-mongodb

我正在尝试从MYSQL获取数据,并在使用java app将其保存在MongoDB中之后。 我的POJO包含用于存储日期字段的java.util.date对象。 当我从MYSQL中获取它时,它返回java.sql.date对象。因此我的pojo将在util date对象中引用sql date。

使用spring数据mongodb我在mongodb中保存相同的pojo。它在异常之下提升:

Exception in thread "main" org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class java.sql.Date.
    at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46)
    at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63)
    at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:37)
    at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:210)
    at com.mongodb.DBObjectCodec.encodeMap(DBObjectCodec.java:220)
    at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:196)
    at com.mongodb.DBObjectCodec.encodeIterable(DBObjectCodec.java:269)
    at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:198)
    at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:128)
    at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:61)
    at com.mongodb.CompoundDBObjectCodec.encode(CompoundDBObjectCodec.java:48)
    at com.mongodb.CompoundDBObjectCodec.encode(CompoundDBObjectCodec.java:27)
    at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63)
    at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29)
    at com.mongodb.connection.UpdateCommandMessage.writeTheWrites(UpdateCommandMessage.java:85)
    at com.mongodb.connection.UpdateCommandMessage.writeTheWrites(UpdateCommandMessage.java:42)
    at com.mongodb.connection.BaseWriteCommandMessage.encodeMessageBodyWithMetadata(BaseWriteCommandMessage.java:118)
    at com.mongodb.connection.RequestMessage.encodeWithMetadata(RequestMessage.java:160)
    at com.mongodb.connection.WriteCommandProtocol.sendMessage(WriteCommandProtocol.java:199)
    at com.mongodb.connection.WriteCommandProtocol.execute(WriteCommandProtocol.java:88)
    at com.mongodb.connection.UpdateCommandProtocol.execute(UpdateCommandProtocol.java:63)
    at com.mongodb.connection.UpdateCommandProtocol.execute(UpdateCommandProtocol.java:37)
    at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:159)
    at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:260)
    at com.mongodb.connection.DefaultServerConnection.updateCommand(DefaultServerConnection.java:121)
    at com.mongodb.operation.UpdateOperation.executeCommandProtocol(UpdateOperation.java:76)
    at com.mongodb.operation.BaseWriteOperation$1.call(BaseWriteOperation.java:111)
    at com.mongodb.operation.BaseWriteOperation$1.call(BaseWriteOperation.java:106)
    at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:187)
    at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:178)
    at com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:106)
    at com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:58)
    at com.mongodb.Mongo.execute(Mongo.java:769)
    at com.mongodb.Mongo$2.execute(Mongo.java:752)
    at com.mongodb.DBCollection.executeWriteOperation(DBCollection.java:330)
    at com.mongodb.DBCollection.replaceOrInsert(DBCollection.java:408)
    at com.mongodb.DBCollection.save(DBCollection.java:397)
    at com.mongodb.DBCollection.save(DBCollection.java:370)
    at org.springframework.data.mongodb.core.MongoTemplate$11.doInCollection(MongoTemplate.java:1040)
    at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:462)
    at org.springframework.data.mongodb.core.MongoTemplate.saveDBObject(MongoTemplate.java:1035)
    at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:975)
    at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:921)
    at com.persivia.ptf.mongo.dao.impl.MongoDaoImpl.save(MongoDaoImpl.java:47)

现在如何在不改变我的dao类的情况下将它保存在mongodb中。

1 个答案:

答案 0 :(得分:1)

尝试使用java.util.Date从MongoDb中恢复时间。我想我记得一个类似的问题并以这种方式解决了。