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;
}
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;
}
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;
}
Aggregations