Search in sources :

Example 26 with MongoPersistentProperty

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

the class ReactiveMongoTemplate method populateIdIfNecessary.

/**
 * Populates the id property of the saved object, if it's not set already.
 *
 * @param savedObject
 * @param id
 */
private void populateIdIfNecessary(Object savedObject, @Nullable Object id) {
    if (id == null) {
        return;
    }
    if (savedObject instanceof Document) {
        Document Document = (Document) savedObject;
        Document.put(ID_FIELD, id);
        return;
    }
    MongoPersistentProperty idProp = getIdPropertyFor(savedObject.getClass());
    if (idProp == null) {
        return;
    }
    ConversionService conversionService = mongoConverter.getConversionService();
    MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(savedObject.getClass());
    PersistentPropertyAccessor accessor = entity.getPropertyAccessor(savedObject);
    if (accessor.getProperty(idProp) != null) {
        return;
    }
    new ConvertingPropertyAccessor(accessor, conversionService).setProperty(idProp, id);
}
Also used : ConvertingPropertyAccessor(org.springframework.data.mapping.model.ConvertingPropertyAccessor) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) ConversionService(org.springframework.core.convert.ConversionService) Document(org.bson.Document) FullDocument(com.mongodb.client.model.changestream.FullDocument) ReturnDocument(com.mongodb.client.model.ReturnDocument)

Example 27 with MongoPersistentProperty

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

the class TypeBasedAggregationOperationContext method getReferenceFor.

private FieldReference getReferenceFor(Field field) {
    PersistentPropertyPath<MongoPersistentProperty> propertyPath = mappingContext.getPersistentPropertyPath(field.getTarget(), type);
    Field mappedField = field(field.getName(), propertyPath.toDotPath(MongoPersistentProperty.PropertyToFieldNameConverter.INSTANCE));
    return new DirectFieldReference(new ExposedField(mappedField, true));
}
Also used : ExposedField(org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) ExposedField(org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField) DirectFieldReference(org.springframework.data.mongodb.core.aggregation.ExposedFields.DirectFieldReference)

Example 28 with MongoPersistentProperty

use of org.springframework.data.mongodb.core.mapping.MongoPersistentProperty 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 29 with MongoPersistentProperty

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

the class MappingMongoConverter method writeAssociation.

private void writeAssociation(Association<MongoPersistentProperty> association, PersistentPropertyAccessor accessor, DocumentAccessor dbObjectAccessor) {
    MongoPersistentProperty inverseProp = association.getInverse();
    writePropertyInternal(accessor.getProperty(inverseProp), dbObjectAccessor, inverseProp);
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)

Example 30 with MongoPersistentProperty

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

the class MappingMongoConverter method readAssociation.

private void readAssociation(Association<MongoPersistentProperty> association, PersistentPropertyAccessor accessor, DocumentAccessor documentAccessor, DbRefProxyHandler handler, DbRefResolverCallback callback) {
    MongoPersistentProperty property = association.getInverse();
    Object value = documentAccessor.get(property);
    if (value == null) {
        return;
    }
    DBRef dbref = value instanceof DBRef ? (DBRef) value : null;
    accessor.setProperty(property, dbRefResolver.resolveDbRef(property, dbref, callback, handler));
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) DBRef(com.mongodb.DBRef) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

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