use of com.mongodb.DBObject in project morphia by mongodb.
the class DatastoreImpl method updateFirst.
@Override
@Deprecated
public <T> UpdateResults updateFirst(final Query<T> query, final T entity, final boolean createIfMissing) {
if (getMapper().getMappedClass(entity).getMappedVersionField() != null) {
throw new UnsupportedOperationException("updateFirst() is not supported with versioned entities");
}
final LinkedHashMap<Object, DBObject> involvedObjects = new LinkedHashMap<Object, DBObject>();
final DBObject dbObj = mapper.toDBObject(entity, involvedObjects);
final UpdateResults res = update(query, dbObj, createIfMissing, false, getWriteConcern(entity));
// update _id field
if (res.getInsertedCount() > 0) {
dbObj.put(Mapper.ID_KEY, res.getNewId());
}
postSaveOperations(singletonList(entity), involvedObjects, getCollection(entity), false);
return res;
}
use of com.mongodb.DBObject in project morphia by mongodb.
the class DatastoreImpl method findAndModify.
@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());
}
use of com.mongodb.DBObject in project morphia by mongodb.
the class DatastoreImpl method merge.
@Override
@SuppressWarnings("unchecked")
public <T> Key<T> merge(final T entity, final WriteConcern wc) {
T unwrapped = entity;
final LinkedHashMap<Object, DBObject> involvedObjects = new LinkedHashMap<Object, DBObject>();
final DBObject dbObj = mapper.toDBObject(unwrapped, involvedObjects);
final Key<T> key = mapper.getKey(unwrapped);
unwrapped = ProxyHelper.unwrap(unwrapped);
final Object id = mapper.getId(unwrapped);
if (id == null) {
throw new MappingException("Could not get id for " + unwrapped.getClass().getName());
}
// remove (immutable) _id field for update.
final Object idValue = dbObj.get(Mapper.ID_KEY);
dbObj.removeField(Mapper.ID_KEY);
WriteResult wr;
final MappedClass mc = mapper.getMappedClass(unwrapped);
final DBCollection dbColl = getCollection(unwrapped);
// try to do an update if there is a @Version field
wr = tryVersionedUpdate(dbColl, unwrapped, dbObj, idValue, new InsertOptions().writeConcern(wc), mc);
if (wr == null) {
final Query<T> query = (Query<T>) createQuery(unwrapped.getClass()).filter(Mapper.ID_KEY, id);
wr = update(query, new BasicDBObject("$set", dbObj), false, false, wc).getWriteResult();
}
final UpdateResults res = new UpdateResults(wr);
if (res.getUpdatedCount() == 0) {
throw new UpdateException("Nothing updated");
}
dbObj.put(Mapper.ID_KEY, idValue);
postSaveOperations(Collections.<Object>singletonList(entity), involvedObjects, dbColl, false);
return key;
}
use of com.mongodb.DBObject in project morphia by mongodb.
the class DatastoreImpl method postSaveOperations.
@SuppressWarnings("unchecked")
private <T> List<Key<T>> postSaveOperations(final Iterable<T> entities, final Map<Object, DBObject> involvedObjects, final DBCollection collection, final boolean fetchKeys) {
List<Key<T>> keys = new ArrayList<Key<T>>();
for (final T entity : entities) {
final DBObject dbObj = involvedObjects.remove(entity);
if (fetchKeys) {
if (dbObj.get(Mapper.ID_KEY) == 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(), collection.getName(), 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;
}
use of com.mongodb.DBObject in project morphia by mongodb.
the class DatastoreImpl method update.
@Override
public <T> UpdateResults update(final Query<T> query, final UpdateOperations<T> operations, final UpdateOptions options) {
DBCollection dbColl = query.getCollection();
// TODO remove this after testing.
if (dbColl == null) {
dbColl = getCollection(query.getEntityClass());
}
final MappedClass mc = getMapper().getMappedClass(query.getEntityClass());
final List<MappedField> fields = mc.getFieldsAnnotatedWith(Version.class);
DBObject queryObject = query.getQueryObject();
if (operations.isIsolated()) {
queryObject.put("$isolated", true);
}
if (!fields.isEmpty()) {
operations.inc(fields.get(0).getNameToStore(), 1);
}
final BasicDBObject update = (BasicDBObject) ((UpdateOpsImpl) operations).getOps();
if (LOG.isTraceEnabled()) {
LOG.trace(format("Executing update(%s) for query: %s, ops: %s, multi: %s, upsert: %s", dbColl.getName(), queryObject, update, options.isMulti(), options.isUpsert()));
}
return new UpdateResults(dbColl.update(queryObject, update, enforceWriteConcern(options, query.getEntityClass()).getOptions()));
}
Aggregations