Search in sources :

Example 1 with Reference

use of dev.morphia.annotations.Reference in project morphia by mongodb.

the class ReferenceCodec method encodeId.

/**
 * Encodes a value
 *
 * @param value the value to encode
 * @param model the mapped class of the field type
 * @return the encoded value
 * @morphia.internal
 */
private Object encodeId(Object value, PropertyModel model) {
    Object idValue;
    final String valueCollectionName;
    if (value instanceof Key) {
        idValue = ((Key<?>) value).getId();
        String collectionName = ((Key<?>) value).getCollection();
        Class<?> type = ((Key<?>) value).getType();
        if (collectionName == null || type == null) {
            throw new QueryException("Missing type or collection information in key");
        }
        valueCollectionName = collectionName;
    } else {
        idValue = mapper.getId(value);
        if (idValue == null) {
            if (!mapper.isMappable(value.getClass())) {
                return value;
            }
            throw new QueryException("No ID value found on referenced entity.  Save referenced entities before defining references to" + " them.");
        }
        valueCollectionName = mapper.getEntityModel(value.getClass()).getCollectionName();
    }
    Reference annotation = model.getAnnotation(Reference.class);
    if (annotation != null && !annotation.idOnly()) {
        idValue = new DBRef(valueCollectionName, idValue);
    }
    return idValue;
}
Also used : QueryException(dev.morphia.query.QueryException) SetReference(dev.morphia.mapping.experimental.SetReference) ListReference(dev.morphia.mapping.experimental.ListReference) MapReference(dev.morphia.mapping.experimental.MapReference) SingleReference(dev.morphia.mapping.experimental.SingleReference) MorphiaReference(dev.morphia.mapping.experimental.MorphiaReference) Reference(dev.morphia.annotations.Reference) DBRef(com.mongodb.DBRef) Key(dev.morphia.Key)

Example 2 with Reference

use of dev.morphia.annotations.Reference in project morphia by mongodb.

the class ReferenceCodec method encodeId.

/**
 * Encodes a value
 *
 * @param mapper
 * @param value  the value to encode
 * @param model  the mapped class of the field type
 * @return the encoded value
 * @morphia.internal
 */
@Nullable
public static Object encodeId(Mapper mapper, Object value, EntityModel model) {
    Object idValue;
    Class<?> type;
    if (value instanceof Key) {
        idValue = ((Key) value).getId();
        String collectionName = ((Key<?>) value).getCollection();
        type = collectionName != null ? mapper.getClassFromCollection(collectionName) : ((Key<?>) value).getType();
        if (type == null) {
            throw new MappingException("The type for the reference could not be determined for the key " + value);
        }
    } else {
        idValue = mapper.getId(value);
        if (idValue == null) {
            return !mapper.isMappable(value.getClass()) ? value : null;
        }
        type = value.getClass();
    }
    String valueCollectionName = mapper.getEntityModel(type).getCollectionName();
    String fieldCollectionName = model.getCollectionName();
    Reference annotation = model.getAnnotation(Reference.class);
    if (annotation != null && !annotation.idOnly() || valueCollectionName != null && !valueCollectionName.equals(fieldCollectionName)) {
        idValue = new DBRef(valueCollectionName, idValue);
    }
    return idValue;
}
Also used : SetReference(dev.morphia.mapping.experimental.SetReference) ListReference(dev.morphia.mapping.experimental.ListReference) MapReference(dev.morphia.mapping.experimental.MapReference) SingleReference(dev.morphia.mapping.experimental.SingleReference) MorphiaReference(dev.morphia.mapping.experimental.MorphiaReference) Reference(dev.morphia.annotations.Reference) DBRef(com.mongodb.DBRef) Key(dev.morphia.Key) MappingException(dev.morphia.mapping.MappingException) Nullable(com.mongodb.lang.Nullable)

Example 3 with Reference

use of dev.morphia.annotations.Reference in project morphia by mongodb.

the class PropertyModelBuilder method discoverMappedName.

public PropertyModelBuilder discoverMappedName() {
    MapperOptions options = mapper.getOptions();
    Property property = getAnnotation(Property.class);
    Reference reference = getAnnotation(Reference.class);
    Version version = getAnnotation(Version.class);
    if (hasAnnotation(Id.class)) {
        mappedName("_id");
    } else if (property != null && !property.value().equals(Mapper.IGNORED_FIELDNAME)) {
        mappedName(property.value());
    } else if (reference != null && !reference.value().equals(Mapper.IGNORED_FIELDNAME)) {
        mappedName(reference.value());
    } else if (version != null && !version.value().equals(Mapper.IGNORED_FIELDNAME)) {
        mappedName(version.value());
    } else {
        mappedName(options.getFieldNaming().apply(name()));
    }
    return this;
}
Also used : MapperOptions(dev.morphia.mapping.MapperOptions) Version(dev.morphia.annotations.Version) Reference(dev.morphia.annotations.Reference) Property(dev.morphia.annotations.Property)

Aggregations

Reference (dev.morphia.annotations.Reference)3 DBRef (com.mongodb.DBRef)2 Key (dev.morphia.Key)2 ListReference (dev.morphia.mapping.experimental.ListReference)2 MapReference (dev.morphia.mapping.experimental.MapReference)2 MorphiaReference (dev.morphia.mapping.experimental.MorphiaReference)2 SetReference (dev.morphia.mapping.experimental.SetReference)2 SingleReference (dev.morphia.mapping.experimental.SingleReference)2 Nullable (com.mongodb.lang.Nullable)1 Property (dev.morphia.annotations.Property)1 Version (dev.morphia.annotations.Version)1 MapperOptions (dev.morphia.mapping.MapperOptions)1 MappingException (dev.morphia.mapping.MappingException)1 QueryException (dev.morphia.query.QueryException)1