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;
}
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);
}
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);
}
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);
}
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;
}
Aggregations