List of usage examples for com.mongodb DBRef DBRef
public DBRef(@Nullable final String databaseName, final String collectionName, final Object id)
From source file:org.grails.datastore.mapping.mongo.engine.MongoEntityPersister.java
License:Apache License
@Override protected void setEmbeddedCollectionKeys(Association association, EntityAccess embeddedEntityAccess, DBObject embeddedEntry, List<Serializable> keys) { List<DBRef> dbRefs = new ArrayList<DBRef>(); for (Object foreignKey : keys) { dbRefs.add(new DBRef((DB) session.getNativeInterface(), getCollectionName(association.getAssociatedEntity()), foreignKey)); }/*from w w w . j a v a 2 s.c o m*/ embeddedEntry.put(association.getName(), dbRefs); }
From source file:org.grails.datastore.mapping.mongo.engine.MongoEntityPersister.java
License:Apache License
@Override protected Object formulateDatabaseReference(PersistentEntity persistentEntity, @SuppressWarnings("rawtypes") Association association, Serializable associationId) { DB db = (DB) session.getNativeInterface(); boolean isReference = isReference(association); if (isReference) return new DBRef(db, getCollectionName(association.getAssociatedEntity()), associationId); else/*from w w w . j a v a 2s .c o m*/ return associationId; }
From source file:org.mule.modules.morphia.MorphiaConnector.java
License:Open Source License
/** * Transforms an entity to a DBRef object * <p/>/*from www .j a v a 2 s. c o m*/ * {@sample.xml ../../../doc/mule-module-morphia.xml.sample morphia:entity-to-dbref} * * @param entity The entity to transform * @param username the username to use in case authentication is required * @param password the password to use in case authentication is required, null * if no authentication is desired * @param host The host of the Mongo server. If the host is part of a replica set then you can specify all the hosts * separated by comma. * @param port The port of the Mongo server * @param database The database name of the Mongo server * @return The DB Reference * @throws ExecutionException if there is a connectivity problem */ @Processor public DBRef entityToDbref(@Optional @Default("#[payload]") Object entity, @Optional String username, @Optional @Password String password, @Optional String host, @Optional Integer port, @Optional String database) throws ExecutionException { Datastore datastore = getDatastore(username, password, database, host, port); Mapper mapper = datastore.getMapper(); Object id = mapper.getId(entity); if (id == null) { throw new IllegalArgumentException("Could not get id from entity"); } return new DBRef(datastore.getDB(), datastore.getCollection(entity.getClass()).getName(), id); }
From source file:org.mule.modules.morphia.MorphiaConnector.java
License:Open Source License
/** * Returns a DBRef object from a given classname and id * <p/>/*from w w w. j a va 2s . com*/ * {@sample.xml ../../../doc/mule-module-morphia.xml.sample morphia:create-dbref} * * * @param className The class of the DBRef * @param id The ID of the DBRef * @param username the username to use in case authentication is required * @param password the password to use in case authentication is required, null * if no authentication is desired * @param host The host of the Mongo server. If the host is part of a replica set then you can specify all the hosts * separated by comma. * @param port The port of the Mongo server * @param database The database name of the Mongo server * @return The DB Reference * @throws ClassNotFoundException If class not found * @throws ExecutionException If there is a connectivity problem */ @Processor public DBRef createDbref(String className, Object id, @Optional String username, @Optional @Password String password, @Optional String host, @Optional Integer port, @Optional String database) throws ClassNotFoundException, ExecutionException { Datastore datastore = getDatastore(username, password, database, host, port); Mapper mapper = datastore.getMapper(); return new DBRef(datastore.getDB(), datastore.getCollection(Class.forName(className)).getName(), id); }
From source file:org.springframework.data.document.mongodb.convert.MappingMongoConverter.java
License:Apache License
protected DBRef createDBRef(Object target, org.springframework.data.document.mongodb.mapping.DBRef dbref) { MongoPersistentEntity<?> targetEntity = mappingContext.getPersistentEntity(target.getClass()); if (null == targetEntity || null == targetEntity.getIdProperty()) { return null; }/*from w ww . j a v a2 s . c o m*/ MongoPersistentProperty idProperty = targetEntity.getIdProperty(); ObjectId id = null; try { id = getProperty(target, idProperty, ObjectId.class, useFieldAccessOnly); if (null == id) { throw new MappingException("Cannot create a reference to an object with a NULL id."); } } catch (IllegalAccessException e) { throw new MappingException(e.getMessage(), e); } catch (InvocationTargetException e) { throw new MappingException(e.getMessage(), e); } String collection = dbref.collection(); if ("".equals(collection)) { collection = targetEntity.getType().getSimpleName().toLowerCase(); } String dbname = dbref.db(); if ("".equals(dbname)) { dbname = defaultDatabase; } DB db = mongo.getDB(dbname); return new DBRef(db, collection, id); }
From source file:org.teiid.translator.mongodb.MergeDetails.java
License:Open Source License
public DBRef getDBRef(DB db, boolean push) { if (this.id != null) { if (this.idReference != null) { return new DBRef(db, push ? this.parentTable : this.embeddedTable, new DBRef(db, this.idReference, this.id.getValue())); }// w w w .ja v a 2s . c om return new DBRef(db, push ? this.parentTable : this.embeddedTable, this.id.getValue()); } return null; }
From source file:org.tinygroup.mongodb.MogoDbTest1.java
License:GNU General Public License
/** * @param args/*from w w w. j a v a 2 s . co m*/ * @throws Throwable */ public static void main(String[] args) throws Throwable { MongoClient connection = new MongoClient(); DB db = connection.getDB("test"); DBCollection collection = db.getCollection("p"); DBObject query = new BasicDBObject(); query.put("_id", new ObjectId("528c53aa5f4089467c5dd15a")); for (DBObject object : collection.find(query).toArray()) { System.out.println(object.toString()); DBRef dbref = new DBRef(db, "p", object.get("p_id")); DBObject refObj = dbref.fetch(); System.out.println(refObj.toString()); } connection.close(); }
From source file:pt.tiago.mongodbteste.MongoDB.java
private void testReferences() { DBCursor cursor = collection.get(1).find(); while (cursor.hasNext()) { DBObject obj = cursor.next();/*www .j a va 2 s. c om*/ BasicDBObject basicObj = (BasicDBObject) obj; Person person = new Person(); person.setID(String.valueOf(basicObj.getObjectId("_id"))); person.setName(basicObj.getString("name")); person.setSurname(basicObj.getString("surname")); DBRef addressRef = new DBRef(db, "Person", basicObj.getObjectId("_id")); DBObject address = addressRef.fetch(); BasicDBObject doc = new BasicDBObject().append("name", person.getName()) .append("surname", person.getSurname()).append("pai", basicObj.getObjectId("_id")); collection.get(1).save(doc); } }
From source file:v7db.files.buckets.BucketsServlet.java
License:Open Source License
private void doEchoPut(HttpServletRequest request, HttpServletResponse response, BSONObject bucket) throws IOException { byte[] sha;//from w w w . j a v a 2s .com BSONObject content = storage.insertContentsAndBackRefs(request.getInputStream(), new DBRef(null, bucketCollection.getName(), bucket.get("_id")), null, null); sha = (byte[]) content.get("sha"); if (sha == null) { sha = ((InlineContent) storage.getContentPointer(content)).getSHA(); } response.setContentType("text/plain"); response.getWriter().write(Hex.encodeHexString(sha)); }