Search in sources :

Example 1 with ReferenceException

use of dev.morphia.mapping.lazy.proxy.ReferenceException in project morphia by mongodb.

the class MorphiaReferenceCodec method encode.

@Override
public void encode(BsonWriter writer, MorphiaReference value, EncoderContext encoderContext) {
    Object ids = value.getId(mapper, getEntityModelForField());
    if (ids == null || (ids instanceof Collection && ((Collection<?>) ids).isEmpty()) || (ids instanceof Map && ((Map<?, ?>) ids).isEmpty())) {
        throw new ReferenceException(Sofia.noIdForReference());
    }
    Codec codec = getDatastore().getCodecRegistry().get(ids.getClass());
    codec.encode(writer, ids, encoderContext);
}
Also used : ReferenceException(dev.morphia.mapping.lazy.proxy.ReferenceException) BaseReferenceCodec(dev.morphia.mapping.codec.BaseReferenceCodec) Codec(org.bson.codecs.Codec) Collection(java.util.Collection) BsonTypeClassMap(org.bson.codecs.BsonTypeClassMap) Map(java.util.Map)

Example 2 with ReferenceException

use of dev.morphia.mapping.lazy.proxy.ReferenceException in project morphia by mongodb.

the class ReferenceCodec method encode.

@Override
public void encode(BsonWriter writer, Object instance, EncoderContext encoderContext) {
    Object idValue = collectIdValues(instance);
    if (idValue != null) {
        final Codec codec = getDatastore().getCodecRegistry().get(idValue.getClass());
        codec.encode(writer, idValue, encoderContext);
    } else {
        throw new ReferenceException(Sofia.noIdForReference());
    }
}
Also used : ReferenceException(dev.morphia.mapping.lazy.proxy.ReferenceException) BaseReferenceCodec(dev.morphia.mapping.codec.BaseReferenceCodec) Codec(org.bson.codecs.Codec)

Example 3 with ReferenceException

use of dev.morphia.mapping.lazy.proxy.ReferenceException in project morphia by mongodb.

the class ReferenceCodec method encode.

@Override
public Object encode(Object value) {
    try {
        DocumentWriter writer = new DocumentWriter(mapper);
        document(writer, () -> {
            writer.writeName("ref");
            encode(writer, value, EncoderContext.builder().build());
        });
        return writer.getDocument().get("ref");
    } catch (ReferenceException e) {
        return value;
    }
}
Also used : ReferenceException(dev.morphia.mapping.lazy.proxy.ReferenceException) DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter)

Aggregations

ReferenceException (dev.morphia.mapping.lazy.proxy.ReferenceException)3 BaseReferenceCodec (dev.morphia.mapping.codec.BaseReferenceCodec)2 Codec (org.bson.codecs.Codec)2 DocumentWriter (dev.morphia.mapping.codec.writer.DocumentWriter)1 Collection (java.util.Collection)1 Map (java.util.Map)1 BsonTypeClassMap (org.bson.codecs.BsonTypeClassMap)1