@Target(value=FIELD) @Retention(value=SOURCE) public @interface Id
The annotation processor automatically marks fields as primary key when the type of the
field is ObjectId or UUID or when the name of field is id
or _id. No need to add this annotation when the defaults are used.
A field annotated with Id has precedence over the automatic id detection.
The primary key will always be stored with the name '_id' in the document.
| Modifier and Type | Optional Element and Description |
|---|---|
Class<? extends org.bson.codecs.Codec<?>> |
codec
A custom
Codec implementation that handles encoding and decoding of the
annotated field. |
Id.IdConversion |
conversion
Specifies a conversion between String and
ObjectId or UUID. |
Class<? extends org.bson.codecs.IdGenerator> |
generator
An implementation of the
IdGenerator interface. |
public abstract Class<? extends org.bson.codecs.IdGenerator> generator
IdGenerator interface. Used during the encode
phase. When the primary key field is null, the Codec calls the generator
and sets the generated value to the field.
The annotation processor automatically uses an ObjectIdGenerator for
primary keys of type ObjectId when the generator() attribute is
not set.
public abstract Id.IdConversion conversion
ObjectId or UUID. When
this attribute is specified the string value will be converted to ObjectId
or UUID before creating the BSON and converted back to a String during the
decode phase.
This attribute is valid for fields with type String.
| Conversion | Java | BSON Document |
Id.IdConversion.BASE64_OBJECTID |
Base64 String | ObjectId |
Id.IdConversion.HEX_OBJECTID |
Hex String | ObjectId |
Id.IdConversion.BASE64_UUID |
Base64 String | UUID |
Id.IdConversion.HEX_UUID |
Hex String | UUID |
public abstract Class<? extends org.bson.codecs.Codec<?>> codec
Codec implementation that handles encoding and decoding of the
annotated field.
When this attribute is specified the annotation processor ignores the
conversion() and generator() attributes.
Copyright © 2017. All rights reserved.