0

I am trying to get data from MYSQL and after processing saving it in MongoDB using java app. My POJO contains java.util.date objects to store date fields. When i fetch from MYSQL it returns java.sql.date object. hence my pojo will have reference of sql date in util date object.

using spring data mongodb i am saving same pojo in mongodb. where it raises below exception:

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)

now how i can save it in mongodb without changing my dao classes.

6
  • Looks more like your Pojo contains java.sql.Date Commented Nov 19, 2015 at 10:21
  • sorry, my pojos are having java.util.date objects. i dont want to change my dao to convert each field like this new java.util.Date(dataRow.getDate("FIELD_DATE").getTime); mongo should handle it itself Commented Nov 19, 2015 at 10:31
  • I am still confused - do you pojos use java.sql.Date or java.util.Date. If the first, I would amend the question accordingly. Commented Nov 19, 2015 at 10:51
  • my POJO uses java.util.date after DB fetch as they return dates as java.sql.date so reference is always of sql date in util date.. hope u got it now. Commented Nov 19, 2015 at 11:01
  • 1
    thank you for elaborating - got it: your Pojo contain java.sql.date object but those are cast to java.util.date. This is where the problem lies - simply convert them properly and the error will go away. Alternatively, you can provide bson with a java.sql.date codec. I would still rephrase the question to point out that java.sql.Date is causing the problem. Commented Nov 19, 2015 at 11:06

1 Answer 1

1

Try to use java.util.Date to recover your time from MongoDb. I think I remember a similar issue and solved it this way.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.