Exemple #1
0
  @Test
  public void testMapOfListsEmb() throws Exception {
    MorphiumSingleton.get().dropCollection(MapListObject.class);

    CMapListObject o = new CMapListObject();
    Map<String, List<EmbObj>> m = new HashMap<String, List<EmbObj>>();
    List<EmbObj> lst = new ArrayList<EmbObj>();
    lst.add(new EmbObj("bla", 12));
    lst.add(new EmbObj("fasel", 42));
    m.put("m1", lst);

    lst = new ArrayList<EmbObj>();
    lst.add(new EmbObj("foo", 42452));
    lst.add(new EmbObj("bar", 2));
    lst.add(new EmbObj("grin", 7331));
    m.put("m2", lst);
    o.setMap4(m);

    MorphiumSingleton.get().store(o);

    Query<CMapListObject> q =
        MorphiumSingleton.get().createQueryFor(CMapListObject.class).f("id").eq(o.getId());
    q.setReadPreferenceLevel(ReadPreferenceLevel.PRIMARY);
    CMapListObject ml = q.get();
    assert (ml.getMap4().get("m1").get(1).getTest().equals("fasel"));
    assert (ml.getMap4().get("m1").get(1).getValue() == 42);
    assert (ml.getMap4().get("m2").get(2).getTest().equals("grin"));
    assert (ml.getMap4().get("m2").get(2).getValue() == 7331);
  }
  @Test
  public void partialUpdateTest() throws Exception {
    MorphiumSingleton.get().clearCollection(PartUpdTestObj.class);
    PartUpdTestObj o = new PartUpdTestObj();
    o.setName("1st");
    o.setValue(5);
    MorphiumSingleton.get().store(o);
    waitForWrites();

    Query<PartUpdTestObj> q = MorphiumSingleton.get().createQueryFor(PartUpdTestObj.class);
    q = q.f("value").eq(5);
    PartUpdTestObj po = q.get();
    assert (po.getValue() == o.getValue()) : "Values different?";
    assert (po.getName().equals(o.getName())) : "Names different?";
    assert (po instanceof PartiallyUpdateable) : "No partial updateable?";
    po.inc();
    assert (((PartiallyUpdateable) po).getAlteredFields().contains("value")) : "Value not changed?";
    MorphiumSingleton.get().store(po);

    po.setName("neuer Name");
    assert (!((PartiallyUpdateable) po).getAlteredFields().contains("value"))
        : "Value still in altered fields?";
    assert (((PartiallyUpdateable) po).getAlteredFields().contains("name")) : "Name not changed?";
    MorphiumSingleton.get().store(po);
    waitForWrites();
    q.setReadPreferenceLevel(ReadPreferenceLevel.PRIMARY);
    po = q.q().f("value").eq(6).get();
    assert (po.getName().equals("neuer Name")) : "Name not changed?";
  }
  public Result morphiumInject() {
    System.out.println("Inside play productcontroller morphium  inject action");
    StringBuffer sb = new StringBuffer();

    Query<Item> q = myMongoDB.getMorphium().createQueryFor(Item.class).f("name").eq("mac");
    List<Item> lst = q.asList();

    for (Item item : lst) {
      System.out.println(item);
      sb.append(item.toString() + "<br>\n");
    }

    return ok("test morphium <br>\n" + sb.toString());
  }
  public Result morphium() {
    System.out.println("Inside play productcontroller morphium action");
    StringBuffer sb = new StringBuffer();
    try {
      MorphiumConfig cfg = new MorphiumConfig();
      cfg.setDatabase("play"); // db name is play
      cfg.addHost("localhost", 27017);
      Morphium m = new Morphium(cfg);

      Query<Item> q = m.createQueryFor(Item.class).f("name").eq("mac");
      List<Item> lst = q.asList();

      for (Item item : lst) {
        System.out.println(item);
        sb.append(item.toString() + "<br>\n");
      }
    } catch (UnknownHostException e) { // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return ok("test morphium <br>\n" + sb.toString());
  }