Search in sources :

Example 1 with MappingException

use of org.mongodb.morphia.mapping.MappingException in project morphia by mongodb.

the class DatastoreImpl method buildExistsQuery.

private Query<?> buildExistsQuery(final Object entityOrKey) {
    final Object unwrapped = ProxyHelper.unwrap(entityOrKey);
    final Key<?> key = mapper.getKey(unwrapped);
    final Object id = key.getId();
    if (id == null) {
        throw new MappingException("Could not get id for " + unwrapped.getClass().getName());
    }
    return find(key.getCollection(), key.getType()).filter(Mapper.ID_KEY, key.getId());
}
Also used : DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) MappingException(org.mongodb.morphia.mapping.MappingException)

Example 2 with MappingException

use of org.mongodb.morphia.mapping.MappingException in project morphia by mongodb.

the class DatastoreImpl method get.

@Override
@SuppressWarnings("unchecked")
public <T> T get(final T entity) {
    final T unwrapped = ProxyHelper.unwrap(entity);
    final Object id = mapper.getId(unwrapped);
    if (id == null) {
        throw new MappingException("Could not get id for " + unwrapped.getClass().getName());
    }
    return (T) get(unwrapped.getClass(), id);
}
Also used : DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) MappingException(org.mongodb.morphia.mapping.MappingException)

Example 3 with MappingException

use of org.mongodb.morphia.mapping.MappingException in project morphia by mongodb.

the class DatastoreImpl method save.

protected <T> Key<T> save(final DBCollection dbColl, final T entity, final InsertOptions options) {
    if (entity == null) {
        throw new UpdateException("Can not persist a null entity");
    }
    final MappedClass mc = mapper.getMappedClass(entity);
    if (mc.getAnnotation(NotSaved.class) != null) {
        throw new MappingException(format("Entity type: %s is marked as NotSaved which means you should not try to save it!", mc.getClazz().getName()));
    }
    // involvedObjects is used not only as a cache but also as a list of what needs to be called for life-cycle methods at the end.
    final LinkedHashMap<Object, DBObject> involvedObjects = new LinkedHashMap<Object, DBObject>();
    final DBObject document = entityToDBObj(entity, involvedObjects);
    // try to do an update if there is a @Version field
    final Object idValue = document.get(Mapper.ID_KEY);
    WriteResult wr = tryVersionedUpdate(dbColl, entity, document, idValue, enforceWriteConcern(options, entity.getClass()), mc);
    if (wr == null) {
        saveDocument(dbColl, document, options);
    }
    return postSaveOperations(singletonList(entity), involvedObjects, dbColl).get(0);
}
Also used : WriteResult(com.mongodb.WriteResult) NotSaved(org.mongodb.morphia.annotations.NotSaved) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) UpdateException(org.mongodb.morphia.query.UpdateException) MappedClass(org.mongodb.morphia.mapping.MappedClass) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) MappingException(org.mongodb.morphia.mapping.MappingException) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with MappingException

use of org.mongodb.morphia.mapping.MappingException 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;
}
Also used : Query(org.mongodb.morphia.query.Query) MappedClass(org.mongodb.morphia.mapping.MappedClass) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) UpdateResults(org.mongodb.morphia.query.UpdateResults) LinkedHashMap(java.util.LinkedHashMap) MappingException(org.mongodb.morphia.mapping.MappingException) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) WriteResult(com.mongodb.WriteResult) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) UpdateException(org.mongodb.morphia.query.UpdateException)

Example 5 with MappingException

use of org.mongodb.morphia.mapping.MappingException 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;
}
Also used : ArrayList(java.util.ArrayList) MappedClass(org.mongodb.morphia.mapping.MappedClass) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) MappingException(org.mongodb.morphia.mapping.MappingException)

Aggregations

MappingException (org.mongodb.morphia.mapping.MappingException)16 DBObject (com.mongodb.DBObject)9 BasicDBObject (com.mongodb.BasicDBObject)8 MappedClass (org.mongodb.morphia.mapping.MappedClass)7 Test (org.junit.Test)3 MappedField (org.mongodb.morphia.mapping.MappedField)3 WriteResult (com.mongodb.WriteResult)2 GenericArrayType (java.lang.reflect.GenericArrayType)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 TypeVariable (java.lang.reflect.TypeVariable)2 WildcardType (java.lang.reflect.WildcardType)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 BsonDocument (org.bson.BsonDocument)2 NotSaved (org.mongodb.morphia.annotations.NotSaved)2 UpdateException (org.mongodb.morphia.query.UpdateException)2 DBCollection (com.mongodb.DBCollection)1 IOException (java.io.IOException)1 BsonString (org.bson.BsonString)1