|
AllAnnotations
All the supported annotations
Featured AnnotationsBelow is a list of all the annotations and a brief descriptions of how to use them. Note: In java the default attribute for an annotations can be used without a name. The name of the default attribute is always value. Any time you see @MyAnnotation("arg") it can be re-written as @MyAnnotation(valeu = "arg"), and must be if you add additional attributes. EntityMarks entities to be stored directly in a collection. Look here for more examples. This annotations is optional in most cases. There is no harm in including it to be more verbose, and make clear the intention for the class. Collection NameAttribute name: value @Entity("name") CappedAttribute name: cap @Entity(cap = @CappedAt(...)) Do not store classnameAttribute name: noClassnameStored @Entity(noClassnameStored = true) This attribute is temporary until polymorphism is completed. IndexesIn addition to being able to declare an index on a single field you can also declare the indexes at the class level. This allows you to create more than just a single field index; it allows you to create compound indexes with multiple fields. Compound IndexesCompound indexes are indexes that include several fields, in a specific order. They are annotated on the the class itself since they include multiple fields. As an example imagine you have a collection that stores changes made by a user. If you want to be able to quickly query recent changes for a given user, you would want to create an index containing both user and date; the order of the fields is significant so please read up on the mongo side of things for optimal performance. @Entity // this is require to know where the indexes are to be created
@Indexes( @Index("user, -date") )
public class ChangeLog{
Date date;
String user;
Record changedRecord;
}
The minus on "-date" indicates that the field is indexed in descending order making it quicker to sort from most recent to oldest. This is the same syntax used when sorting or ordering results from a query. Say you decided you also needed to be able to quickly find recent changes for the record being changed. You could change the annotation to: @Indexes({
@Index("user, -cs"),
@Index("changedRecord, -cs")})And now you will have two compound indexes on that collection. If you like you can also define single field indexes in this fashion as well; it is recommended that you don't since it is more error prone. IdMarks a field in an @Entity to be the "id" field in mongodb. @Entity
class Myclass {
@Id ObjectId id = new ObjectId();
...
}PropertyTransientThe field with not be persisted. SerializedThe field with be converted to binary, and persisted. NotSavedThe field will not be saved, but can be loaded. Good for data migration. AlsoLoadThe field will can be loaded as any of the supplied names. Good for data migration. IndexedThe field will be indexed. See the datastore docs. VersionMarks a field in an @Entity to be control optimistic locking for that entity. It will be automatically managed for you. There is no need to set a value. @Entity
class Myclass {
...
@Version Long v;
}ReferenceMarks fields as stored in another collection and which are linked (by a dbref reference field). When the Entity is loaded, so is the (direct) reference. Attribute name: lazy Instead of loading the referenced field with the Entity, it will be lazily loaded on the first method call of the proxy instance. Attribute name: ignoreMissing When loading bad references won't generate an exception Attribute name: concreteClass The class type to create for these references Look here for more examples. EmbeddedAllows customization of certain options. Look here for more examples. Lifecycle AnnotationsSee the this page. | |
How to set compound index with 2d ?
Entity also has another useful attribute that is not yet documented:
Attribute name: concern
@Entity(concern = "SAFE")
there are many annotations (like @Polimorhpic or @Version (0.99)) which are not documented...