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

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

Introduction

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

Prototype

boolean isDbReference();

Source Link

Document

Returns whether the property is a com.mongodb.DBRef .

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;// w w w .j a  v  a 2  s . 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.
 * /*  w  w w  .  j  a  v a 2  s .  co 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;
}