com.morphiatest.businesslogic.App.java Source code

Java tutorial

Introduction

Here is the source code for com.morphiatest.businesslogic.App.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.morphiatest.businesslogic;

import com.morphiatest.genericdataobjects.Vegetable;
import com.mongodb.MongoClient;
import java.net.UnknownHostException;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import org.mongodb.morphia.query.Query;

/**
 *
 * @author CZC
 */
public class App {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws UnknownHostException {
        // given
        Dish dish = new Dish();
        Vegetable vegetable = new Vegetable();
        vegetable.setName("pea");
        vegetable.setOriginFarm("Maggies Farm");
        dish.setMainIngredient(vegetable);

        // when
        Morphia morphia = new Morphia();
        MongoClient client = new MongoClient();
        Datastore datastore = morphia.createDatastore(client, "test");

        datastore.getDB().dropDatabase();

        datastore.save(vegetable);
        datastore.save(dish);

        Query<Dish> query = datastore.createQuery(Dish.class);
        query.get();

        // then in console
        // WARNING: Class com.morphiatest.genericdataobjects.Ingredient is stored in the 'Ingredient' collection but a reference was found for this type to another collection, 'Vegetable'. The reference will be loaded using the class anyway. { "$ref" : "Vegetable", "$id" : "548de3d0389751c97913091c" }
    }

}