Search in sources :

Example 6 with MappingException

use of org.springframework.data.mapping.MappingException in project spring-data-mongodb by spring-projects.

the class BasicMongoPersistentEntity method returnPropertyIfBetterIdPropertyCandidateOrNull.

/**
 * As a general note: An implicit id property has a name that matches "id" or "_id". An explicit id property is one
 * that is annotated with @see {@link Id}. The property id is updated according to the following rules: 1) An id
 * property which is defined explicitly takes precedence over an implicitly defined id property. 2) In case of any
 * ambiguity a @see {@link MappingException} is thrown.
 *
 * @param property - the new id property candidate
 * @return
 */
@Override
protected MongoPersistentProperty returnPropertyIfBetterIdPropertyCandidateOrNull(MongoPersistentProperty property) {
    Assert.notNull(property, "MongoPersistentProperty must not be null!");
    if (!property.isIdProperty()) {
        return null;
    }
    MongoPersistentProperty currentIdProperty = getIdProperty();
    boolean currentIdPropertyIsSet = currentIdProperty != null;
    @SuppressWarnings("null") boolean currentIdPropertyIsExplicit = currentIdPropertyIsSet ? currentIdProperty.isExplicitIdProperty() : false;
    boolean newIdPropertyIsExplicit = property.isExplicitIdProperty();
    if (!currentIdPropertyIsSet) {
        return property;
    }
    @SuppressWarnings("null") Field currentIdPropertyField = currentIdProperty.getField();
    if (newIdPropertyIsExplicit && currentIdPropertyIsExplicit) {
        throw new MappingException(String.format("Attempt to add explicit id property %s but already have an property %s registered " + "as explicit id. Check your mapping configuration!", property.getField(), currentIdPropertyField));
    } else if (newIdPropertyIsExplicit && !currentIdPropertyIsExplicit) {
        // explicit id property takes precedence over implicit id property
        return property;
    } else if (!newIdPropertyIsExplicit && currentIdPropertyIsExplicit) {
    // no id property override - current property is explicitly defined
    } else {
        throw new MappingException(String.format("Attempt to add id property %s but already have an property %s registered " + "as id. Check your mapping configuration!", property.getField(), currentIdPropertyField));
    }
    return null;
}
Also used : Field(java.lang.reflect.Field) MappingException(org.springframework.data.mapping.MappingException)

Example 7 with MappingException

use of org.springframework.data.mapping.MappingException in project spring-data-mongodb by spring-projects.

the class MongoTemplate method extractIdPropertyAndValue.

/**
 * Returns {@link Entry} containing the field name of the id property as {@link Entry#getKey()} and the {@link Id}s
 * property value as its {@link Entry#getValue()}.
 *
 * @param object
 * @return
 */
private Pair<String, Object> extractIdPropertyAndValue(Object object) {
    Assert.notNull(object, "Id cannot be extracted from 'null'.");
    Class<?> objectType = object.getClass();
    if (object instanceof Document) {
        return Pair.of(ID_FIELD, ((Document) object).get(ID_FIELD));
    }
    MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(objectType);
    if (entity != null && entity.hasIdProperty()) {
        MongoPersistentProperty idProperty = entity.getIdProperty();
        return Pair.of(idProperty.getFieldName(), entity.getPropertyAccessor(object).getProperty(idProperty));
    }
    throw new MappingException("No id property found for object of type " + objectType);
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) Document(org.bson.Document) MappingException(org.springframework.data.mapping.MappingException)

Example 8 with MappingException

use of org.springframework.data.mapping.MappingException in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplate method extractIdPropertyAndValue.

/**
 * Returns {@link Entry} containing the field name of the id property as {@link Entry#getKey()} and the {@link Id}s
 * property value as its {@link Entry#getValue()}.
 *
 * @param object
 * @return
 */
private Pair<String, Object> extractIdPropertyAndValue(Object object) {
    Assert.notNull(object, "Id cannot be extracted from 'null'.");
    Assert.notNull(object, "Id cannot be extracted from 'null'.");
    Class<?> objectType = object.getClass();
    if (object instanceof Document) {
        return Pair.of(ID_FIELD, ((Document) object).get(ID_FIELD));
    }
    MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(objectType);
    if (entity != null && entity.hasIdProperty()) {
        MongoPersistentProperty idProperty = entity.getIdProperty();
        return Pair.of(idProperty.getFieldName(), entity.getPropertyAccessor(object).getProperty(idProperty));
    }
    throw new MappingException("No id property found for object of type " + objectType);
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) Document(org.bson.Document) FullDocument(com.mongodb.client.model.changestream.FullDocument) ReturnDocument(com.mongodb.client.model.ReturnDocument) MappingException(org.springframework.data.mapping.MappingException)

Example 9 with MappingException

use of org.springframework.data.mapping.MappingException in project spring-data-mongodb by spring-projects.

the class MappingMongoConverter method writeInternal.

protected void writeInternal(@Nullable Object obj, final Bson bson, MongoPersistentEntity<?> entity) {
    if (obj == null) {
        return;
    }
    if (null == entity) {
        throw new MappingException("No mapping metadata found for entity of type " + obj.getClass().getName());
    }
    PersistentPropertyAccessor accessor = entity.getPropertyAccessor(obj);
    DocumentAccessor dbObjectAccessor = new DocumentAccessor(bson);
    MongoPersistentProperty idProperty = entity.getIdProperty();
    if (idProperty != null && !dbObjectAccessor.hasValue(idProperty)) {
        Object value = idMapper.convertId(accessor.getProperty(idProperty));
        if (value != null) {
            dbObjectAccessor.put(idProperty, value);
        }
    }
    writeProperties(bson, entity, accessor, dbObjectAccessor, idProperty);
}
Also used : PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) MappingException(org.springframework.data.mapping.MappingException)

Example 10 with MappingException

use of org.springframework.data.mapping.MappingException in project spring-data-mongodb by spring-projects.

the class MappingMongoConverter method writeMapInternal.

/**
 * Writes the given {@link Map} to the given {@link Document} considering the given {@link TypeInformation}.
 *
 * @param obj must not be {@literal null}.
 * @param bson must not be {@literal null}.
 * @param propertyType must not be {@literal null}.
 * @return
 */
protected Bson writeMapInternal(Map<Object, Object> obj, Bson bson, TypeInformation<?> propertyType) {
    for (Map.Entry<Object, Object> entry : obj.entrySet()) {
        Object key = entry.getKey();
        Object val = entry.getValue();
        if (conversions.isSimpleType(key.getClass())) {
            String simpleKey = prepareMapKey(key);
            if (val == null || conversions.isSimpleType(val.getClass())) {
                writeSimpleInternal(val, bson, simpleKey);
            } else if (val instanceof Collection || val.getClass().isArray()) {
                addToMap(bson, simpleKey, writeCollectionInternal(asCollection(val), propertyType.getMapValueType(), new BasicDBList()));
            } else {
                Document document = new Document();
                TypeInformation<?> valueTypeInfo = propertyType.isMap() ? propertyType.getMapValueType() : ClassTypeInformation.OBJECT;
                writeInternal(val, document, valueTypeInfo);
                addToMap(bson, simpleKey, document);
            }
        } else {
            throw new MappingException("Cannot use a complex object as a key value.");
        }
    }
    return bson;
}
Also used : BasicDBList(com.mongodb.BasicDBList) Collection(java.util.Collection) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Document(org.bson.Document) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ClassTypeInformation(org.springframework.data.util.ClassTypeInformation) TypeInformation(org.springframework.data.util.TypeInformation) MappingException(org.springframework.data.mapping.MappingException)

Aggregations

MappingException (org.springframework.data.mapping.MappingException)12 Document (org.bson.Document)8 BasicDBObject (com.mongodb.BasicDBObject)6 DBObject (com.mongodb.DBObject)5 MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)5 BasicDBList (com.mongodb.BasicDBList)3 Collection (java.util.Collection)3 DBRef (com.mongodb.DBRef)2 ReturnDocument (com.mongodb.client.model.ReturnDocument)2 FullDocument (com.mongodb.client.model.changestream.FullDocument)2 JSONParseException (com.mongodb.util.JSONParseException)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 Field (java.lang.reflect.Field)1 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)1 ClassTypeInformation (org.springframework.data.util.ClassTypeInformation)1 TypeInformation (org.springframework.data.util.TypeInformation)1 Nullable (org.springframework.lang.Nullable)1