-
Notifications
You must be signed in to change notification settings - Fork 43
DOCSP-18711: Explain BsonRepresentation limitation #549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -287,6 +287,10 @@ package: | |||||
- Specifies the BSON type used to store the value when different from the | ||||||
POJO property. | ||||||
|
||||||
.. seealso:: | ||||||
|
||||||
:ref:`bsonrepresentation-annotation-code-example` | ||||||
|
||||||
* - ``BsonId`` | ||||||
- Marks a property to serialize as the _id property. | ||||||
|
||||||
|
@@ -441,6 +445,59 @@ following data: | |||||
additionalInfo=Document{{dimensions=3x4x5, weight=256g}} | ||||||
] | ||||||
|
||||||
.. _bsonrepresentation-annotation-code-example: | ||||||
|
||||||
BsonRepresentation Error Example | ||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||
|
||||||
The ``@BsonRepresentation`` annotation allows you to store a POJO class field | ||||||
as a different data type in your MongoDB database. The :ref:`Product POJO | ||||||
<bson-annotation-code-example>` code example in the :ref:`annotations` section | ||||||
of this page uses ``@BsonRepresentation`` to store ``String`` values as | ||||||
``ObjectId`` values in the database documents. | ||||||
|
||||||
However, using the ``@BsonRepresentation`` annotation to convert between data types other | ||||||
than ``String`` and ``ObjectId`` causes the following error message: | ||||||
|
||||||
.. code-block:: | ||||||
:copyable: false | ||||||
|
||||||
Codec must implement RepresentationConfigurable to support BsonRepresentation | ||||||
|
||||||
For example, the following code adds a ``purchaseDate`` field of type ``Long`` to the | ||||||
``Product`` POJO. This example attempts to represent ``Long`` values as ``DateTime`` | ||||||
values in the database: | ||||||
|
||||||
.. code-block:: java | ||||||
:emphasize-lines: 9-10 | ||||||
|
||||||
public class Product { | ||||||
@BsonProperty("modelName") | ||||||
private String name; | ||||||
|
||||||
@BsonId() | ||||||
@BsonRepresentation(BsonType.OBJECT_ID) | ||||||
private String serialNumber; | ||||||
|
||||||
@BsonRepresentation(BsonType.DATE_TIME) | ||||||
private Long purchaseDate; | ||||||
|
||||||
// ... | ||||||
} | ||||||
|
||||||
The preceding code results in an error. Instead, you can create a custom Codec to | ||||||
convert the ``purchaseDate`` values from type ``Long`` to ``DateTime``: | ||||||
|
||||||
.. literalinclude:: /includes/fundamentals/code-snippets/LongCodec.java | ||||||
:language: java | ||||||
:start-after: start class | ||||||
:end-before: end class | ||||||
|
||||||
Then, you can add an instance of the ``LongCodec`` to your ``CodecRegistry``, which contains | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
a mapping between your Codec and the Java object type to which it applies. For instructions | ||||||
on registering your custom Codec with the ``CodecRegistry``, see the :ref:`fundamentals-codecs` | ||||||
guide. | ||||||
|
||||||
.. _pojo-discriminators: | ||||||
|
||||||
Discriminators | ||||||
|
@@ -699,4 +756,3 @@ codec registry. | |||||
|
||||||
See the documentation on the :ref:`default codec registry <codecs-default-codec-registry>` | ||||||
For more information about how to register the codecs it includes. | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package fundamentals; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to tech reviewer: I'm a little uncertain about this example code. The example provided in the ticket didn't convert between Long and Datetime, so I wrote my own example. Let me know if this needs to be updated! |
||
|
||
import org.bson.BsonReader; | ||
import org.bson.BsonWriter; | ||
import org.bson.codecs.Codec; | ||
import org.bson.codecs.DecoderContext; | ||
import org.bson.codecs.EncoderContext; | ||
|
||
// start class | ||
public class LongCodec implements Codec<Long> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would convert all Longs to a Date times, which wouldn't be desired by users.
Requires a Codec that implements
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great, thanks for providing this code! |
||
|
||
@Override | ||
public void encode(BsonWriter writer, Long value, EncoderContext encoderContext) { | ||
if (value != null) { | ||
writer.writeDateTime(value); | ||
} | ||
} | ||
|
||
@Override | ||
public Long decode(BsonReader reader, DecoderContext decoderContext) { | ||
return reader.readDateTime(); | ||
} | ||
|
||
@Override | ||
public Class<Long> getEncoderClass() { | ||
return Long.class; | ||
} | ||
} | ||
// end class |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestoin for clarity: