Jeff Yemin will host a webinar covering the design and major features of Morphia, an Object Document Mapper (ODM) for Java and MongoDB. This webinar will start with a short introduction to MongoDB and the various options for building MongoDB applications on the JVM before taking a deep dive into Morphia. Morphia will be presented as an extended example format that demonstrates, for each feature, the domain model, a test driver, and the results as they appear in MongoDB.
36. Small schema change (model)
abstract class Member {
@Id String userName;
@Property("memberSince") Date since;
boolean active;
String name;
}
@Entity("programmers")
class Programmer extends Member {
int followers;
List<String> following;
}
@Entity("orgs")
class Organization extends Member {
}
@Entity("repos")
class Repository {
@Id ObjectId id;
@Reference Member owner;
String name;
@Reference(lazy=true) Repository forkedFrom;
}
37. Small schema change (test)
Programmer scott = new Programmer();
//…
ds.save(scott);
// save mongodb Organization
Organization mongodb = new Organization("mongodb",
"mongodb", sdf.parse("Jan 8, 2009"));
ds.save(mongodb);
// save mongodb's docs Repository
Repository mongoDocs = new Repository(mongodb, "docs");
ds.save(mongoDocs);
// save Scott's forked docs Repository
Repository scottDocs = new Repository(scott, "docs",
mongoDocs);
ds.save(scottDocs);
50. @Indexed
• Annotation for fields
– value (IndexDirection)
– name (String)
– unique (boolean)
– dropDups (boolean)
– background (boolean)
– sparse (boolean)
• Examples
– @Indexed(value=IndexDirection.ASC, name="followers") int followers;
– @Indexed @Reference Repository forkedFrom;
51. @Indexes and @Index
• @Indexes: Annotation for types
– value (Index[])
• @Index
– value (String)
– Others same as @Indexed
• Examples
– @Indexes(@Index("since, -followers")) public class
Programmer {…}
53. Updating with save (test)
Programmer jeff = createJeff();
ds.save(jeff);
// jeff is following scott, so increment
// scott's followers and re-save
Programmer scott = ds.get(Programmer.class,
"scotthernandez")
scott.followers++;
ds.save(scott);
56. Optimistic Concurrency (model)
@Entity
public abstract class Member {
@Id String userName;
@Property("memberSince") Date since;
boolean active;
String name;
@Version Long version;
}
61. Web Resources
• Morphia home: http://code.google.com/p/morphia/
• Morphia user group:
https://groups.google.com/forum/?fromgroups#!forum/morphia
• Demo code: https://github.com/jyemin/morphia-demo
– Separate commit and tag for each slide, so you can play
along
Builds on the java driver"test" is the name of a database'
NameEmilMember SinceFollowersRespositories
No annotations. Pure POJO
1. Save will insert or update
Collection name defaults to class name_id is generated as an ObjectIdclassName has the full class name, including package. Used for polymorphic collectionCan you query for base class? How?
1. Morphia will generate an id and stuff it in the field annotated with @Id
1. If you already have a unique id, you should use it as your @Id
1. Morphia won't generate these for you
1. If you just use @Entity, it defaults the collection name to the class name.
Note the collection name
Let's switch it up. Instead of embedding the repos in the programmer, let's have repos as top-level docs that reference owner by key
Get by primary keyShortcut for getting by a single propertyLongcut, same as above but extensible
1. Note the weird syntax to make followers descending
Note that to update followers, we updated everything…Point out the problems with this