[JAVA-1741] 3.x: How to save java.sql.Timestamp or java.sql.Date? Created: 02/Apr/15 Updated: 18/Jul/16 Resolved: 05/Jan/16 |
|
| Status: | Closed |
| Project: | Java Driver |
| Component/s: | Codecs |
| Affects Version/s: | 3.0.0 |
| Fix Version/s: | 3.2.1 |
| Type: | Improvement | Priority: | Major - P3 |
| Reporter: | Ngoc | Assignee: | Jeffrey Yemin |
| Resolution: | Done | Votes: | 1 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||||||||||
| Description |
|
I've followed the tutorial Getting Started with the 3.0 Java Driver: But when I save a doc with a java.sql.Timestamp value, there's error about codec. I'm used to 2.x driver, but new to 3.x driver. Can you update the tutorial to instruct how to save java.sql.Timestamp? |
| Comments |
| Comment by Jeffrey Yemin [ 13/Jan/16 ] | ||||||||||||||||
|
Closing for 3.2.1 release. | ||||||||||||||||
| Comment by Githook User [ 05/Jan/16 ] | ||||||||||||||||
|
Author: {u'username': u'jyemin', u'name': u'Jeff Yemin', u'email': u'jeff.yemin@10gen.com'}Message: Note: for java.sql.Timestamp in particular, this can result in information loss, as the nanos field is silently ignored Note: As this is only for backwards compatibility, support for encoding Date subclasses has only been added for DBObject, and not other containers like Document. | ||||||||||||||||
| Comment by Githook User [ 04/Jan/16 ] | ||||||||||||||||
|
Author: {u'username': u'jyemin', u'name': u'Jeff Yemin', u'email': u'jeff.yemin@10gen.com'}Message: Note: for java.sql.Timestamp in particular, this can result in information loss, as the nanos field is silently ignored Note: As this is only for backwards compatibility, support for encoding Date subclasses has only been added for DBObject, and not other containers like Document. | ||||||||||||||||
| Comment by Alexander Komyagin [ 22/Oct/15 ] | ||||||||||||||||
|
Here's one workaround to get a MongoTemplate in Spring with the converter in place:
and
| ||||||||||||||||
| Comment by Jeffrey Yemin [ 02/Sep/15 ] | ||||||||||||||||
|
Yes, you're right, if you're using the DBCollection API. Sorry for the confusion. | ||||||||||||||||
| Comment by Gui Forget [ 02/Sep/15 ] | ||||||||||||||||
|
Thanks Jeff. I don't believe we can register custom Codec for the DBObjectCodec though, that codec solely use the DefaultCodecRegistry | ||||||||||||||||
| Comment by Jeffrey Yemin [ 02/Sep/15 ] | ||||||||||||||||
|
It's a mistake that the 2.x driver encodes instances of java.sql.Timestamp, as it silently drops the nanosecond field that's present in that class. We plan to document this backward compatibility break, and suggest that users prepare for this by converting Timestamp to Date, e.g. new Date(timestamp.getTime()). If that's not possible, then an application can always register a custom Codec, as Ross described in an earlier comment. | ||||||||||||||||
| Comment by Gui Forget [ 01/Sep/15 ] | ||||||||||||||||
|
Adding it to the Default registry is key so it works also for the DBObjectCodec. After I upgraded to 3.0 I'm getting this error and as far as I know I have no way to fix it other than converting before encoding the DBObject:
| ||||||||||||||||
| Comment by Ross Lawley [ 23/Jun/15 ] | ||||||||||||||||
|
Good question - I've reopened and will get back shortly. | ||||||||||||||||
| Comment by Ngoc [ 23/Jun/15 ] | ||||||||||||||||
|
Thanks for the info. | ||||||||||||||||
| Comment by Ross Lawley [ 23/Jun/15 ] | ||||||||||||||||
|
Hi, Apologies for the delay, we have added a section on the documentation site regarding Codecs and Codec Registries - http://mongodb.github.io/mongo-java-driver/3.0/bson/codecs/ If you follow that you will be able to create a codec for a java.sql.Timestamp. The steps are: 1. Create a Codec for the Timestamp
5. Pass the new CodecRegistry to the MongoClientOptions.builder and use when creating a new MongoClient OR pass the CodecRegistry and use the MongoDatabase.withCodecRegistry. Hope that clarifies the process. |