@SuppressWarnings("unchecked") private <T> List<Key<T>> postSaveOperations(final Iterable<T> entities, final Map<Object, DBObject> involvedObjects, final boolean fetchKeys, final String collectionName) { List<Key<T>> keys = new ArrayList<Key<T>>(); for (final T entity : entities) { final DBObject dbObj = involvedObjects.remove(entity); if (fetchKeys) { if (dbObj.get("_id") == null) { throw new MappingException(format("Missing _id after save on %s", entity.getClass().getName())); } mapper.updateKeyAndVersionInfo(this, dbObj, createCache(), entity); keys.add(new Key<T>((Class<? extends T>) entity.getClass(), collectionName, mapper.getId(entity))); } mapper.getMappedClass(entity).callLifecycleMethods(PostPersist.class, entity, dbObj, mapper); } for (Entry<Object, DBObject> entry : involvedObjects.entrySet()) { final Object key = entry.getKey(); mapper.getMappedClass(key).callLifecycleMethods(PostPersist.class, key, entry.getValue(), mapper); } return keys; }
@Override public <T> T get(final Class<T> clazz, final DBRef ref) { DBObject object = getDB().getCollection(ref.getCollectionName()).findOne(new BasicDBObject("_id", ref.getId())); return mapper.fromDBObject(this, clazz, object, createCache()); }
@Override public <T> T findAndModify(final Query<T> query, final UpdateOperations<T> operations, final FindAndModifyOptions options) { DBCollection dbColl = query.getCollection(); // TODO remove this after testing. if (dbColl == null) { dbColl = getCollection(query.getEntityClass()); } if (LOG.isTraceEnabled()) { LOG.info("Executing findAndModify(" + dbColl.getName() + ") with update "); } updateForVersioning(query, operations); DBObject res = dbColl.findAndModify(query.getQueryObject(), options.copy() .sort(query.getSortObject()) .projection(query.getFieldsObject()) .update(((UpdateOpsImpl<T>) operations).getOps()) .getOptions()); return res == null ? null : mapper.fromDBObject(this, query.getEntityClass(), res, createCache()); }
@Override public <T> T findAndDelete(final Query<T> query, final FindAndModifyOptions options) { DBCollection dbColl = query.getCollection(); if (dbColl == null) { dbColl = getCollection(query.getEntityClass()); } if (LOG.isTraceEnabled()) { LOG.trace("Executing findAndModify(" + dbColl.getName() + ") with delete ..."); } FindAndModifyOptions copy = enforceWriteConcern(options, query.getEntityClass()) .copy() .projection(query.getFieldsObject()) .sort(query.getSortObject()) .returnNew(false) .upsert(false) .remove(true); final DBObject result = dbColl.findAndModify(query.getQueryObject(), copy.getOptions()); return result == null ? null : mapper.fromDBObject(this, query.getEntityClass(), result, createCache()); }
@Override public <T> MapreduceResults<T> mapReduce(final MapReduceOptions<T> options) { DBCollection collection = options.getQuery().getCollection(); final EntityCache cache = createCache(); MapreduceResults<T> results = new MapreduceResults<T>(collection.mapReduce(options.toCommand(getMapper()))); results.setOutputType(options.getOutputType()); if (OutputType.INLINE.equals(options.getOutputType())) { results.setInlineRequiredOptions(this, options.getResultType(), getMapper(), cache); } else { results.setQuery(newQuery(options.getResultType(), getDB().getCollection(results.getOutputCollectionName()))); } return results; }
final EntityCache cache = createCache(); MapreduceResults<T> results = new MapreduceResults<T>(dbColl.mapReduce(baseCommand));