save entity using morphia

We saw the introduction to morphia in previous article. From now on we will see morphia in action. Let us try to define and save entity using morphia.

Save Entity Using Morphia:

Set up Morphia:

First of all we need to include morphia jar along with all required jars specified in setting up mongodb project. You can download the source from morphia github and build the jar using your favourite building tool e.g. maven or gradle. You will see a jar file created called morphia.1.1.0.jar. Include jar in your eclipse project.

Create an Entity:

Employee is save entity using morphia. Create Employee entity as below.

1234567891011121314151617181920212223package com.thejavageek.morphia.entities; import org.bson.types.ObjectId;import org.mongodb.morphia.annotations.Entity;import org.mongodb.morphia.annotations.Id;import org.mongodb.morphia.annotations.Property; @Entity(value = “employees”)public class Employee {  @Id private ObjectId id; private String name; @Property(value = “dept”) private String department;  public ObjectId getId() { return id; }  // getters and setters for name and department }

Things to notice here

  • @Entity annotation specifies that we want to save entity using morphia.
  • @Entity(value = “employees”) specifies the collection in which entities need to be saved
  • @Id annotation is used to specify the default _id created in mongodb.
  • @Property is used to map java member variable to document field in mongodb. If you do not specify @Property annotation, then morphia will create the field in document with same name as that of java member variable
  • @Property (value = “dept”) specifies that department value should be stored in field called dept

Initialize Morphia:

Now that we have defined entity, let us initialize and save entity using morphia. Consider code below

1234MongoClient client = new MongoClient(“localhost”, 27017);Morphia morphia = new Morphia();Datastore ds = morphia.createDatastore(client, “employee_db”);morphia.map(Employee.class);
  • new MongoClient(“localhost”,27017); instantiates client
  • new Morphia(); instantiates morphia.
  • morphia.createDataStore(client, “employee_db”); connect to server and create datastore for employee_db
  • morphia.map(Employee.class); maps Employee entity to employees collection as specified in entity annotation.

Now create and save entity using morphia.

12345Employee harish = new Employee();harish.setName(“Harish Taware”);harish.setDepartment(“IT”); ds.save(harish);

This inserts a document in mongodb. Let us check it.

123456{    “_id” : ObjectId(“55db1432c2610a1e2c19866b”),    “className” : “com.thejavageek.morphia.entities.Employee”,    “name” : “Harish Taware”,    “dept” : “IT”}

Note that four fields are created

  • _id which is default identifier for each document
  • className is a default field added by morphia, which denotes the type of class in java.
  • name is saved as it is because java entity has member variable name
  • Field dept is created for member variable department because we have explicitly specified using @Property(value = “dept”)

I hope the article helped understand how to save entity using morphia. Next articles will deal with other operations those can be performed in morphia.