save entity using morphia

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

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.

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

  • 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.

This inserts a document in mongodb. Let us check 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.

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

One thought on “save entity using morphia

  • September 3, 2015 at 12:16 pm
    Permalink

    There is no validation on the field expression so you can put anything in there that is valid on the server. This query will be used in the samples to restrict the update operations to only the hotel we just created.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *