Example usage for org.springframework.data.mongodb.core.mapping MongoPersistentProperty getDBRef

List of usage examples for org.springframework.data.mongodb.core.mapping MongoPersistentProperty getDBRef

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core.mapping MongoPersistentProperty getDBRef.

Prototype

@Nullable
DBRef getDBRef();

Source Link

Document

Returns the DBRef if the property is a reference.

Usage

From source file:org.springframework.data.mongodb.core.convert.MappingMongoConverter.java

@SuppressWarnings({ "unchecked" })
protected void writePropertyInternal(Object obj, DBObject dbo, MongoPersistentProperty prop) {

    if (obj == null) {
        return;/*from   w ww . j a va  2s  .  c  o m*/
    }

    String name = prop.getFieldName();
    TypeInformation<?> valueType = ClassTypeInformation.from(obj.getClass());
    TypeInformation<?> type = prop.getTypeInformation();

    if (valueType.isCollectionLike()) {
        DBObject collectionInternal = createCollection(asCollection(obj), prop);
        dbo.put(name, collectionInternal);
        return;
    }

    if (valueType.isMap()) {
        BasicDBObject mapDbObj = new BasicDBObject();
        writeMapInternal((Map<Object, Object>) obj, mapDbObj, type);
        dbo.put(name, mapDbObj);
        return;
    }

    if (prop.isDbReference()) {
        DBRef dbRefObj = createDBRef(obj, prop.getDBRef());
        if (null != dbRefObj) {
            dbo.put(name, dbRefObj);
            return;
        }
    }

    // Lookup potential custom target type
    Class<?> basicTargetType = conversions.getCustomWriteTarget(obj.getClass(), null);

    if (basicTargetType != null) {
        dbo.put(name, conversionService.convert(obj, basicTargetType));
        return;
    }

    BasicDBObject propDbObj = new BasicDBObject();
    addCustomTypeKeyIfNecessary(type, obj, propDbObj);

    MongoPersistentEntity<?> entity = isSubtype(prop.getType(), obj.getClass())
            ? mappingContext.getPersistentEntity(obj.getClass())
            : mappingContext.getPersistentEntity(type);

    writeInternal(obj, propDbObj, entity);
    dbo.put(name, propDbObj);
}

From source file:org.springframework.data.mongodb.core.convert.MappingMongoConverter.java

/**
 * Writes the given {@link Collection} using the given {@link MongoPersistentProperty} information.
 * //from   www. jav a2  s  . c  o m
 * @param collection must not be {@literal null}.
 * @param property must not be {@literal null}.
 * @return
 */
protected DBObject createCollection(Collection<?> collection, MongoPersistentProperty property) {

    if (!property.isDbReference()) {
        return writeCollectionInternal(collection, property.getTypeInformation(), new BasicDBList());
    }

    BasicDBList dbList = new BasicDBList();

    for (Object element : collection) {

        if (element == null) {
            continue;
        }

        DBRef dbRef = createDBRef(element, property.getDBRef());
        dbList.add(dbRef);
    }

    return dbList;
}