Search in sources :

Example 1 with NotSaved

use of org.mongodb.morphia.annotations.NotSaved 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 2 with NotSaved

use of org.mongodb.morphia.annotations.NotSaved in project morphia by mongodb.

the class DatastoreImpl method toDbObject.

private <T> DBObject toDbObject(final T ent, final Map<Object, DBObject> involvedObjects) {
    final MappedClass mc = mapper.getMappedClass(ent);
    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()));
    }
    DBObject dbObject = entityToDBObj(ent, involvedObjects);
    List<MappedField> versionFields = mc.getFieldsAnnotatedWith(Version.class);
    for (MappedField mappedField : versionFields) {
        String name = mappedField.getNameToStore();
        if (dbObject.get(name) == null) {
            dbObject.put(name, 1);
            mappedField.setFieldValue(ent, 1L);
        }
    }
    return dbObject;
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) NotSaved(org.mongodb.morphia.annotations.NotSaved) MappedClass(org.mongodb.morphia.mapping.MappedClass) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) MappingException(org.mongodb.morphia.mapping.MappingException)

Aggregations

BasicDBObject (com.mongodb.BasicDBObject)2 DBObject (com.mongodb.DBObject)2 NotSaved (org.mongodb.morphia.annotations.NotSaved)2 MappedClass (org.mongodb.morphia.mapping.MappedClass)2 MappingException (org.mongodb.morphia.mapping.MappingException)2 WriteResult (com.mongodb.WriteResult)1 LinkedHashMap (java.util.LinkedHashMap)1 MappedField (org.mongodb.morphia.mapping.MappedField)1 UpdateException (org.mongodb.morphia.query.UpdateException)1