use of dev.morphia.query.QueryException 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;
}
Aggregations