Package com.mongodb

Examples of com.mongodb.DBRef


        assertEquals(object.toString(), object2.toString());
    }

    @Test
    public void testSerializeDBRef() throws Exception {
        DBRef dbRef = new DBRef(getDatabase(), "col", 42);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);

        objectOutputStream.writeObject(dbRef);

        ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
        ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
        DBRef dbRef2 = (DBRef) objectInputStream.readObject();

        assertEquals(dbRef, dbRef2);
    }
View Full Code Here


        final DBObject rDbObject = getMorphia().toDBObject(r);
        rDbObject.put("_ns", rectangles.getName());
        rectangles.save(rDbObject);

        final ContainsRef cRef = new ContainsRef();
        cRef.rect = new DBRef(getDb(), (String) rDbObject.get("_ns"), rDbObject.get("_id"));
        final DBObject cRefDbObject = getMorphia().toDBObject(cRef);
        stuff.save(cRefDbObject);
        final BasicDBObject cRefDbObjectLoaded = (BasicDBObject) stuff.findOne(BasicDBObjectBuilder.start("_id", cRefDbObject.get("_id"))
                                                                                                   .get());
        final ContainsRef cRefLoaded = getMorphia().fromDBObject(ContainsRef.class, cRefDbObjectLoaded, new DefaultEntityCache());
View Full Code Here

    public <T, V> DBRef createRef(final Class<T> clazz, final V id) {
        if (id == null) {
            throw new MappingException("Could not get id for " + clazz.getName());
        }
        return new DBRef(getDB(), getCollection(clazz).getName(), id);
    }
View Full Code Here

        }
        if (key.getKind() == null) {
            key.setKind(getCollectionName(key.getKindClass()));
        }

        return new DBRef(getDatastoreProvider().get().getDB(), key.getKind(), key.getId());
    }
View Full Code Here

        }
    }

    boolean exists(final Class c, final Object ref, final EntityCache cache, final Mapper mapper, final boolean idOnly) {
        final Key key = idOnly ? mapper.manualRefToKey(c, ref) : mapper.refToKey((DBRef) ref);
        final DBRef dbRef = idOnly ? null : (DBRef) ref;
        final Boolean cached = cache.exists(key);
        if (cached != null) {
            return cached;
        }

        final DatastoreImpl dsi = (DatastoreImpl) mapper.getDatastoreProvider().get();

        final DBCollection dbColl = dsi.getCollection(c);
        if (!idOnly && !dbColl.getName().equals(dbRef.getRef())) {
            LOG.warning("Class " + c.getName() + " is stored in the '" + dbColl.getName()
                        + "' collection but a reference was found for this type to another collection, '" + dbRef.getRef()
                        + "'. The reference will be loaded using the class anyway. " + dbRef);
        }
        final boolean exists;
        if (idOnly) {
            exists = (dsi.find(dbColl.getName(), c).disableValidation().filter("_id", ref).asKeyList().size() == 1);
        } else {
            exists = (dsi.find(dbRef.getRef(), c).disableValidation().filter("_id", dbRef.getId()).asKeyList().size() == 1);
        }
        cache.notifyExists(key, exists);
        return exists;
    }
View Full Code Here

                         final boolean idOnly) {
        if (ref == null) {
            return null;
        }

        final DBRef dbRef = idOnly ? null : (DBRef) ref;
        final Key key = mapper.createKey(mf.isSingleValue() ? mf.getType() : mf.getSubClass(),
                                         idOnly ? ref : dbRef.getId());
        final Datastore ds = idOnly ? mapper.getDatastoreProvider().get() : null;

        final Object cached = cache.getEntity(key);
        if (cached != null) {
            return cached;
        }

        //TODO: if _db is null, set it?
        final DBObject refDbObject = idOnly ? ds.getCollection(key.getKindClass()).findOne(ref) : dbRef.fetch();

        if (refDbObject != null) {
            Object refObj = mapper.getOptions().getObjectFactory().createInstance(mapper, mf, refDbObject);
            refObj = mapper.fromDb(refDbObject, refObj, cache);
            cache.putEntity(key, refObj);
View Full Code Here

      if (Mirror.me(ele.getClass()).isObj()) {
        if (field.isRef()) {
          MongoEntity en = Mongos.entity(val.getClass().getComponentType());
          DBObject dbo = en.toDBObject(ele);
          if (dbo.containsField("_id")) {
            array[i] = new DBRef(null, en.getCollectionName(null), dbo.get("_id"));
            continue;
          } else {
            if (log.isWarnEnabled())
              log.warn("!!obj without _id but using as ref field value!! fallback to embed doc!!");
          }
View Full Code Here

    if (!dbo.containsField("_id")) {
      if (log.isWarnEnabled())
        log.warn("!!obj without _id but using as ref field value!! fallback to embed doc!!");
      return dbo;
    }
    return new DBRef(null, en.getCollectionName(null), dbo.get("_id"));
  }
View Full Code Here

    answer.setUpdatedAt(new Date());
    dao.runNoError(new Callback<DB>() {
      public void invoke(DB db) {
        dao.save(answer);
        Moo moo = Moo.NEW();
        moo.push("answers", new DBRef(db, "answer", new ObjectId(answer.getId())));
        dao.update(Question.class, Moo.NEW("id", questionId), moo);
      }
    });
    commons.fresh(Question.class, questionId);
    return Ajax.ok();
View Full Code Here

  }
 
  /**监视一个问题,问题被修改的时候(添加删除答案等),发出提醒*/
  @At("/question/?/watch")
  public void watch(String questionId, @Attr("me") User me) {
    dao.update(Question.class, new BasicDBObject("_id", new ObjectId(questionId)), new BasicDBObject("$addToSet", new BasicDBObject("watchers", new DBRef(null, "user", new ObjectId(me.getId())))));
  }
View Full Code Here

TOP

Related Classes of com.mongodb.DBRef

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.