`
leonzhx
  • 浏览: 793745 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Annotations

阅读更多

1.   In java the default attribute for 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(value = "arg") , and must be if you add additional attributes.

 

 

2.   @Entity Marks entities to be stored directly in a collection. This annotation is optional in most cases. There is no harm in including it to be more verbose, and make clear the intention for the class.

 

3.   You can optionally set a name for your MongoDB DBCollection name. You will also need a (no-args) default constructor(it’s not necessarily public):

@Entity("hotels")
public class Hotel {
...
    public Hotel() {
    }
...
}
 

 

4.   The @Entity annotation provides another optional parameter to not store the class name in the document:

 

@Entity(value="hotels", noClassnameStored=true)
 

The default behavior is to store the class name in the document. This is mainly used when storing different entities in the same collection and reading them back as the base or super class:

@Entity("animals") abstract class Animal { String name; }
@Entity("animals") Cat extends Animal { ... }
@Entity("animals") Dog extends Animal { ... }

//And then performing the following query...
List<Animal> animals = ds.createQuery(Animal.class).asList(); 
 

 

Without the class name stored in the document, Morphia wouldn't know which class to actually create. If you are only storing a single entity type in the collection and you are concerned about datasize, it would be safe not to store the classname.

 

5.   Classes annotated with @Entity require unique @Id values; these values are stored in the MongoDB "id" field, which has a unique index requirement. Mongo will generate the Id for your new objects, so you don't need to worry about that. It will be stored as an ObjectId ; If you use any other type than ObjectId you must set the value yourself.

 

6.   @Indexed applies an index to a field. The indexes are applied when the datastore.ensureIndexes() method is called:

@Indexed(value=IndexDirection.ASC, name="upc", unique=true, dropDups=true)
 

 

The parameters are:

value : Indicates the direction of the index; IndexDirection.ASC (ascending), IndexDirection.DESC (descending), IndexDirection.BOTH (both), default is ascending

name : The name of the index to create; default is to let the mongodb create a name (in the form of key1_1/-1_key2_1/-1...

unique : Creates the index as a unique value index; inserting duplicates values in this field will cause errors, default false

dropDups : Tells the unique index to drop duplicates silently when creating; only the first will be kept. default false.

 

7.   Morphia can save and load classes without default constructor:

morphia.getMapper().getOptions().objectFactory=new DefaultCreator() {
  @Override
  public Object createInstance(Class clazz,
                             com.mongodb.DBObject dbObj) {
    if(clazz.equals(ThirdPartyClass.class)) {
        return new ThirdPartyClass(dbObj.get("something_required"));
    }
    return super.createInstance(clazz,dbObj);
  }
};
 

 

8.   You can also choose to create a class that will be embedded in the Entity, we use the @Embedded to annotate the embedded field in the Enity and the class for that field. The class annotated with @Embedded is not allowed to have an @Id .

 

9.   In 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 compound indexes with multiple fields:

 

@Entity // this is require to know where the indexes are to be created
@Indexes({
   @Index("user, -cs"),
   @Index("changedRecord, -cs")})

public class ChangeLog{
 Date date;
 String user;
 Record changedRecord;
} 
 

 

10.   Those fields tagged with @Transient will not be persisted. Those fields tagged with @Serialized will be converted to binary, and persisted. Those fields tagged with @NotSaved will not be saved, but can be loaded. Those fields tagged with @AlsoLoad can be loaded as any of the supplied names.

 

11.   A field tagged with @Version is to control optimistic locking for the containing entity. If the versions change while modifying an entity a ConcurrentModificationException will be throw in the write method (save/delete/etc). This field will be automatically managed for you -- there is no need to set a value and you should not do so anyway

@Entity
class Myclass {
   ...
   @Version Long v;
}
 

 

 

12.   @Reference marks 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. It supports following attribute:

    a)   lazy : Instead of loading the referenced field with the Entity, it will be lazily loaded on the first method call of the proxy instance.

    b)  ignoreMissing : When loading bad references won't generate an exception.

    c)   concreteClass : The class type to create for these references.

 

13.   The referenced object must have been saved in Mongo before saving the object referencing it.

 

14.  There are various annotations which can be used to register callbacks on certain lifecycle events. These include Pre/Post-Persist (Save), and Pre/Post-Load.

    a)   @PrePersist - Called before save, it can return a DBObject in place of an empty one.

    b)   @PostPersist - Called after the save call to the datastore

    c)   @PreLoad - Called before mapping the datastore object to the entity (POJO); the DBObject is passed as an argument (you can add/remove/change values)

    d)   @PostLoad - Called after mapping to the entity

 

15.   All parameters and return values are options in your implemented callbacks.

 

16.   Here is a simple example of an entity that always saves the Date it was last updated at:

class BankAccount {
  @Id String id;
  Date lastUpdated = new Date();

  @PrePersist void prePersist() {lastUpdated = new Date();}
}
 

 

 

17.   You can separate the lifecycle event implementation in an external class, or many:

@EntityListeners(BackAccountWatcher.class)
class BankAccount {
  @Id String id;
  Date lastUpdated = new Date();
}

class BankAccountWatcher{
 
  @PrePersist void prePersist(BankAccount act) {act.lastUpdated = new Date();}

}
 

 

 

18.   There is no way to support a Delete lifecycle event.

 

19.   By default, Morphia will try to map all the supported basic and primitive types to/from Mongo. MongoDB only has the following data types:

    a)   Integer (32-bit signed value)

    b)  Long (64-bit signed value)

    c)   Double (64-bit IEEE754 fp value)

    d)  String

 

20.   Morphia can also store a java.util.List , java.util.Set , and java.util.Map collections, and primitive arrays of any supported types. Morphia will use the following implementations (by default) when creating collections:

    a)  java.util.ArrayList for List

    b)  java.util.HashSet for Set

    c)  java.util.HashMap for Map

If you want to use another implementation, you can override this by the concreteClass attribute of @Property /@Embedded /@Reference annotations.

分享到:
评论

相关推荐

    annotations-13.0-API文档-中文版.zip

    赠送jar包:annotations-13.0.jar; 赠送原API文档:annotations-13.0-javadoc.jar; 赠送源代码:annotations-13.0-sources.jar; 赠送Maven依赖信息文件:annotations-13.0.pom; 包含翻译后的API文档:...

    annotations-23.0.0-API文档-中文版.zip

    赠送jar包:annotations-23.0.0.jar; 赠送原API文档:annotations-23.0.0-javadoc.jar; 赠送源代码:annotations-23.0.0-sources.jar; 赠送Maven依赖信息文件:annotations-23.0.0.pom; 包含翻译后的API文档:...

    swagger-annotations-1.5.20-API文档-中文版.zip

    赠送jar包:swagger-annotations-1.5.20.jar; 赠送原API文档:swagger-annotations-1.5.20-javadoc.jar; 赠送源代码:swagger-annotations-1.5.20-sources.jar; 赠送Maven依赖信息文件:swagger-annotations-...

    swagger-annotations-2.1.2-API文档-中文版.zip

    赠送jar包:swagger-annotations-2.1.2.jar; 赠送原API文档:swagger-annotations-2.1.2-javadoc.jar; 赠送源代码:swagger-annotations-2.1.2-sources.jar; 赠送Maven依赖信息文件:swagger-annotations-2.1.2....

    swagger-annotations-1.6.2-API文档-中文版.zip

    赠送jar包:swagger-annotations-1.6.2.jar; 赠送原API文档:swagger-annotations-1.6.2-javadoc.jar; 赠送源代码:swagger-annotations-1.6.2-sources.jar; 赠送Maven依赖信息文件:swagger-annotations-1.6.2....

    audience-annotations-0.5.0-API文档-中英对照版.zip

    赠送jar包:audience-annotations-0.5.0.jar; 赠送原API文档:audience-annotations-0.5.0-javadoc.jar; 赠送源代码:audience-annotations-0.5.0-sources.jar; 赠送Maven依赖信息文件:audience-annotations-...

    jackson-annotations-2.9.0-API文档-中文版.zip

    赠送jar包:jackson-annotations-2.9.0.jar; 赠送原API文档:jackson-annotations-2.9.0-javadoc.jar; 赠送源代码:jackson-annotations-2.9.0-sources.jar; 赠送Maven依赖信息文件:jackson-annotations-2.9.0....

    j2objc-annotations-1.3-API文档-中文版.zip

    赠送jar包:j2objc-annotations-1.3.jar; 赠送原API文档:j2objc-annotations-1.3-javadoc.jar; 赠送源代码:j2objc-annotations-1.3-sources.jar; 赠送Maven依赖信息文件:j2objc-annotations-1.3.pom; 包含...

    swagger-annotations-1.5.24-API文档-中文版.zip

    赠送jar包:swagger-annotations-1.5.24.jar; 赠送原API文档:swagger-annotations-1.5.24-javadoc.jar; 赠送源代码:swagger-annotations-1.5.24-sources.jar; 赠送Maven依赖信息文件:swagger-annotations-...

    jackson-annotations-2.13.1-API文档-中文版.zip

    赠送jar包:jackson-annotations-2.13.1.jar; 赠送原API文档:jackson-annotations-2.13.1-javadoc.jar; 赠送源代码:jackson-annotations-2.13.1-sources.jar; 赠送Maven依赖信息文件:jackson-annotations-...

    error_prone_annotations-2.3.2-API文档-中文版.zip

    赠送jar包:error_prone_annotations-2.3.2.jar; 赠送原API文档:error_prone_annotations-2.3.2-javadoc.jar; 赠送源代码:error_prone_annotations-2.3.2-sources.jar; 赠送Maven依赖信息文件:error_prone_...

    error_prone_annotations-2.10.0-API文档-中英对照版.zip

    赠送jar包:error_prone_annotations-2.10.0.jar; 赠送原API文档:error_prone_annotations-2.10.0-javadoc.jar; 赠送源代码:error_prone_annotations-2.10.0-sources.jar; 赠送Maven依赖信息文件:error_prone_...

    jackson-annotations-2.11.4-API文档-中文版.zip

    赠送jar包:jackson-annotations-2.11.4.jar; 赠送原API文档:jackson-annotations-2.11.4-javadoc.jar; 赠送源代码:jackson-annotations-2.11.4-sources.jar; 赠送Maven依赖信息文件:jackson-annotations-...

    j2objc-annotations-1.1-API文档-中文版.zip

    赠送jar包:j2objc-annotations-1.1.jar; 赠送原API文档:j2objc-annotations-1.1-javadoc.jar; 赠送源代码:j2objc-annotations-1.1-sources.jar; 赠送Maven依赖信息文件:j2objc-annotations-1.1.pom; 包含...

    error_prone_annotations-2.5.1-API文档-中文版.zip

    赠送jar包:error_prone_annotations-2.5.1.jar; 赠送原API文档:error_prone_annotations-2.5.1-javadoc.jar; 赠送源代码:error_prone_annotations-2.5.1-sources.jar; 赠送Maven依赖信息文件:error_prone_...

    error_prone_annotations-2.2.0-API文档-中文版.zip

    赠送jar包:error_prone_annotations-2.2.0.jar; 赠送原API文档:error_prone_annotations-2.2.0-javadoc.jar; 赠送源代码:error_prone_annotations-2.2.0-sources.jar; 赠送Maven依赖信息文件:error_prone_...

    error_prone_annotations-2.0.18-API文档-中文版.zip

    赠送jar包:error_prone_annotations-2.0.18.jar; 赠送原API文档:error_prone_annotations-2.0.18-javadoc.jar; 赠送源代码:error_prone_annotations-2.0.18-sources.jar; 赠送Maven依赖信息文件:error_prone_...

    error_prone_annotations-2.3.4-API文档-中文版.zip

    赠送jar包:error_prone_annotations-2.3.4.jar; 赠送原API文档:error_prone_annotations-2.3.4-javadoc.jar; 赠送源代码:error_prone_annotations-2.3.4-sources.jar; 赠送Maven依赖信息文件:error_prone_...

    jackson-module-jaxb-annotations-2.7.8-API文档-中英对照版.zip

    赠送jar包:jackson-module-jaxb-annotations-2.7.8.jar; 赠送原API文档:jackson-module-jaxb-annotations-2.7.8-javadoc.jar; 赠送源代码:jackson-module-jaxb-annotations-2.7.8-sources.jar; 赠送Maven依赖...

    error_prone_annotations-2.1.3-API文档-中文版.zip

    赠送jar包:error_prone_annotations-2.1.3.jar; 赠送原API文档:error_prone_annotations-2.1.3-javadoc.jar; 赠送源代码:error_prone_annotations-2.1.3-sources.jar; 赠送Maven依赖信息文件:error_prone_...

Global site tag (gtag.js) - Google Analytics