Search in sources :

Example 6 with MongoPersistentProperty

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

the class MappingMongoConverter method read.

@Nullable
private <S extends Object> S read(final MongoPersistentEntity<S> entity, final Document bson, final ObjectPath path) {
    DefaultSpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator(bson, spELContext);
    ParameterValueProvider<MongoPersistentProperty> provider = getParameterProvider(entity, bson, evaluator, path);
    EntityInstantiator instantiator = instantiators.getInstantiatorFor(entity);
    S instance = instantiator.createInstance(entity, provider);
    PersistentPropertyAccessor accessor = new ConvertingPropertyAccessor(entity.getPropertyAccessor(instance), conversionService);
    MongoPersistentProperty idProperty = entity.getIdProperty();
    DocumentAccessor documentAccessor = new DocumentAccessor(bson);
    // make sure id property is set before all other properties
    Object idValue = null;
    if (idProperty != null && documentAccessor.hasValue(idProperty)) {
        idValue = readIdValue(path, evaluator, idProperty, documentAccessor);
        accessor.setProperty(idProperty, idValue);
    }
    ObjectPath currentPath = path.push(instance, entity, idValue != null ? bson.get(idProperty.getFieldName()) : null);
    MongoDbPropertyValueProvider valueProvider = new MongoDbPropertyValueProvider(documentAccessor, evaluator, currentPath);
    DbRefResolverCallback callback = new DefaultDbRefResolverCallback(bson, currentPath, evaluator, MappingMongoConverter.this);
    readProperties(entity, accessor, idProperty, documentAccessor, valueProvider, callback);
    return instance;
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) EntityInstantiator(org.springframework.data.convert.EntityInstantiator) DefaultSpELExpressionEvaluator(org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator) ConvertingPropertyAccessor(org.springframework.data.mapping.model.ConvertingPropertyAccessor) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Nullable(org.springframework.lang.Nullable)

Example 7 with MongoPersistentProperty

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

the class MappingMongoConverter method createDBRef.

protected DBRef createDBRef(Object target, MongoPersistentProperty property) {
    Assert.notNull(target, "Target object must not be null!");
    if (target instanceof DBRef) {
        return (DBRef) target;
    }
    MongoPersistentEntity<?> targetEntity = mappingContext.getPersistentEntity(target.getClass());
    targetEntity = targetEntity != null ? targetEntity : mappingContext.getPersistentEntity(property);
    if (null == targetEntity) {
        throw new MappingException("No mapping metadata found for " + target.getClass());
    }
    MongoPersistentEntity<?> entity = targetEntity;
    MongoPersistentProperty idProperty = entity.getIdProperty();
    if (idProperty != null) {
        Object id = target.getClass().equals(idProperty.getType()) ? target : entity.getPropertyAccessor(target).getProperty(idProperty);
        if (null == id) {
            throw new MappingException("Cannot create a reference to an object with a NULL id.");
        }
        return dbRefResolver.createDbRef(property == null ? null : property.getDBRef(), entity, idMapper.convertId(id));
    }
    throw new MappingException("No id property found on class " + entity.getType());
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) DBRef(com.mongodb.DBRef) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) MappingException(org.springframework.data.mapping.MappingException)

Example 8 with MongoPersistentProperty

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

the class MongoExampleMapper method getMappedPropertyPath.

private String getMappedPropertyPath(String path, Class<?> probeType) {
    MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(probeType);
    Iterator<String> parts = Arrays.asList(path.split("\\.")).iterator();
    final Stack<MongoPersistentProperty> stack = new Stack<>();
    List<String> resultParts = new ArrayList<>();
    while (parts.hasNext()) {
        String part = parts.next();
        MongoPersistentProperty prop = entity.getPersistentProperty(part);
        if (prop == null) {
            entity.doWithProperties((PropertyHandler<MongoPersistentProperty>) property -> {
                if (property.getFieldName().equals(part)) {
                    stack.push(property);
                }
            });
            if (stack.isEmpty()) {
                return "";
            }
            prop = stack.pop();
        }
        resultParts.add(prop.getName());
        if (prop.isEntity() && mappingContext.hasPersistentEntityFor(prop.getActualType())) {
            entity = mappingContext.getRequiredPersistentEntity(prop.getActualType());
        } else {
            break;
        }
    }
    return StringUtils.collectionToDelimitedString(resultParts, ".");
}
Also used : Document(org.bson.Document) MongoRegexCreator(org.springframework.data.mongodb.core.query.MongoRegexCreator) Arrays(java.util.Arrays) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) StringMatcher(org.springframework.data.domain.ExampleMatcher.StringMatcher) MappingContext(org.springframework.data.mapping.context.MappingContext) TypeInformation(org.springframework.data.util.TypeInformation) Stack(java.util.Stack) UntypedExampleMatcher(org.springframework.data.mongodb.core.query.UntypedExampleMatcher) ArrayList(java.util.ArrayList) PropertyHandler(org.springframework.data.mapping.PropertyHandler) ExampleMatcherAccessor(org.springframework.data.support.ExampleMatcherAccessor) HashSet(java.util.HashSet) NullHandler(org.springframework.data.domain.ExampleMatcher.NullHandler) Map(java.util.Map) MongoPersistentEntity(org.springframework.data.mongodb.core.mapping.MongoPersistentEntity) MatchMode(org.springframework.data.mongodb.core.query.MongoRegexCreator.MatchMode) ClassUtils(org.springframework.util.ClassUtils) Iterator(java.util.Iterator) SerializationUtils(org.springframework.data.mongodb.core.query.SerializationUtils) ObjectUtils(org.springframework.util.ObjectUtils) Set(java.util.Set) Example(org.springframework.data.domain.Example) List(java.util.List) PropertyValueTransformer(org.springframework.data.domain.ExampleMatcher.PropertyValueTransformer) Entry(java.util.Map.Entry) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) ArrayList(java.util.ArrayList) Stack(java.util.Stack)

Example 9 with MongoPersistentProperty

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

the class MongoPersistentEntityIndexResolver method resolveAndAddIndexesForAssociation.

private void resolveAndAddIndexesForAssociation(Association<MongoPersistentProperty> association, List<IndexDefinitionHolder> indexes, String path, String collection) {
    MongoPersistentProperty property = association.getInverse();
    String propertyDotPath = (StringUtils.hasText(path) ? path + "." : "") + property.getFieldName();
    if (property.isAnnotationPresent(GeoSpatialIndexed.class) || property.isAnnotationPresent(TextIndexed.class)) {
        throw new MappingException(String.format("Cannot create geospatial-/text- index on DBRef in collection '%s' for path '%s'.", collection, propertyDotPath));
    }
    IndexDefinitionHolder indexDefinitionHolder = createIndexDefinitionHolderForProperty(propertyDotPath, collection, property);
    if (indexDefinitionHolder != null) {
        indexes.add(indexDefinitionHolder);
    }
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) MappingException(org.springframework.data.mapping.MappingException)

Example 10 with MongoPersistentProperty

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

the class MongoPersistentEntityIndexCreatorUnitTests method doesNotCreateIndexForEntityComingFromDifferentMappingContext.

@Test
public void doesNotCreateIndexForEntityComingFromDifferentMappingContext() {
    MongoMappingContext mappingContext = new MongoMappingContext();
    MongoMappingContext personMappingContext = prepareMappingContext(Person.class);
    MongoPersistentEntityIndexCreator creator = new MongoPersistentEntityIndexCreator(mappingContext, mongoTemplate);
    MongoPersistentEntity<?> entity = personMappingContext.getRequiredPersistentEntity(Person.class);
    MappingContextEvent<MongoPersistentEntity<?>, MongoPersistentProperty> event = new MappingContextEvent<MongoPersistentEntity<?>, MongoPersistentProperty>(personMappingContext, entity);
    creator.onApplicationEvent(event);
    verifyZeroInteractions(collection);
}
Also used : MappingContextEvent(org.springframework.data.mapping.context.MappingContextEvent) MongoPersistentEntity(org.springframework.data.mongodb.core.mapping.MongoPersistentEntity) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) Test(org.junit.Test)

Aggregations

MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)40 Document (org.bson.Document)13 DBRef (com.mongodb.DBRef)10 Test (org.junit.Test)9 MappingException (org.springframework.data.mapping.MappingException)6 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)6 ConvertingPropertyAccessor (org.springframework.data.mapping.model.ConvertingPropertyAccessor)6 MongoPersistentEntity (org.springframework.data.mongodb.core.mapping.MongoPersistentEntity)6 BasicDBObject (com.mongodb.BasicDBObject)5 DBObject (com.mongodb.DBObject)4 ReturnDocument (com.mongodb.client.model.ReturnDocument)4 FullDocument (com.mongodb.client.model.changestream.FullDocument)4 Arrays (java.util.Arrays)3 Iterator (java.util.Iterator)3 ObjectId (org.bson.types.ObjectId)3 InvalidDataAccessApiUsageException (org.springframework.dao.InvalidDataAccessApiUsageException)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 List (java.util.List)2