- 浏览: 793745 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (651)
- Java (39)
- Java 初学者小问题 (66)
- 设计模式 (7)
- 项目管理 (3)
- 数据库 (1)
- 算法 (2)
- Java practices (6)
- Effective Java2读书笔记 (78)
- Linux (2)
- programming ruby 读书笔记 (5)
- Core Java Ninth Edition Volume I 读书笔记 (15)
- Pro Git 读书笔记 (12)
- Git (3)
- Maven in Action 读书笔记 (20)
- Web (12)
- 非技术类书籍 (11)
- 电影 (40)
- Web Cache (1)
- jquery (0)
- 历史 (4)
- Dive Into HTML5 读书笔记 (13)
- 三国演义小学毕业考 (79)
- 高效能人士的7个习惯 读书笔记 (12)
- Java Performance 读书笔记 (3)
- Protocol Buffer 学习笔记 (6)
- Mongo DB 学习笔记 (7)
- Morphia 学习笔记 (7)
- Algorithms -- Princeton 学习笔记 (13)
- String研究 (10)
- Hadoop: The Definitive Guide 读书笔记 (3)
- Java与模式读书笔记 (5)
- Date研究 (3)
- The Roman Empire 听课笔记 (4)
- Algorithms -- Standford 学习笔记 (16)
- Core Java Ninth Edition Volume II 读书笔记 (9)
- Thinking in Java 4th Edition 读书笔记 (21)
- Node : Up and Running 学习笔记 (5)
- Eloquent Javascript (8)
- Smashing Node.js 读书笔记 (1)
- Algorithms II -- Standford 学习笔记 (19)
- Algorithm II -- Princeton 学习笔记 (14)
- 网络安全 (2)
- Javascript (4)
- 正则表达式 (1)
- JAVA 7/8 (15)
- JVM (10)
- NodeJS (1)
- 鸟哥的linux私房菜读书笔记 (14)
- Web Service (1)
- The art of programming (9)
- Introduction to Algorithm 读书笔记 (4)
- Java 源码阅读 (0)
- Spring in Action 读书笔记 (2)
- Java Network Programming 读书笔记 (2)
最新评论
-
心存高远:
谢谢作者分享,刚好看到这里不太明白,现在茅塞顿开。不过runt ...
关于 Maven的传递依赖的理解 -
sxlkk:
851228082 写道甚至在某次技术会议现场遇到《Maven ...
关于 Maven的传递依赖的理解 -
851228082:
851228082 写道a----compile----b-- ...
第五章 坐标和依赖 -
851228082:
a----compile----b-----provided- ...
第五章 坐标和依赖 -
851228082:
甚至在某次技术会议现场遇到《Maven in action》的 ...
关于 Maven的传递依赖的理解
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.
发表评论
-
About how to manually map part of the fields of the annoted class with Morphia
2012-09-24 20:02 1715Today , I met with a problem th ... -
Advanced Usage
2012-09-01 16:40 7241. Once we've annotated ... -
Morphia always stores classname for embedded collection
2012-08-29 23:21 2192Today, I found a problem that M ... -
Query
2012-08-27 14:33 10241. The Query interfa ... -
Datastore
2012-08-26 17:15 10411. The Datastore int ... -
Quick Start
2012-08-20 16:39 9491. Morphia depends ...
相关推荐
赠送jar包:annotations-13.0.jar; 赠送原API文档:annotations-13.0-javadoc.jar; 赠送源代码:annotations-13.0-sources.jar; 赠送Maven依赖信息文件:annotations-13.0.pom; 包含翻译后的API文档:...
赠送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文档:...
赠送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-...
赠送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....
赠送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....
赠送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-...
赠送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....
赠送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; 包含...
赠送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-...
赠送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-...
赠送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_...
赠送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_...
赠送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-...
赠送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; 包含...
赠送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_...
赠送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_...
赠送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_...
赠送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_...
赠送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依赖...
赠送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_...