/** * Writes the given {@link Collection} using the given {@link MongoPersistentProperty} information. * * @param collection must not be {@literal null}. * @param property must not be {@literal null}. * @return */ protected List<Object> createCollection(Collection<?> collection, MongoPersistentProperty property) { if (!property.isDbReference()) { return writeCollectionInternal(collection, property.getTypeInformation(), new BasicDBList()); } List<Object> dbList = new ArrayList<>(collection.size()); for (Object element : collection) { if (element == null) { continue; } DBRef dbRef = createDBRef(element, property); dbList.add(dbRef); } return dbList; }
private static void potentiallyAssertDBRefTargetType(MongoPersistentProperty persistentProperty) { if (persistentProperty.isDbReference() && persistentProperty.getDBRef().lazy()) { if (persistentProperty.isArray() || Modifier.isFinal(persistentProperty.getActualType().getModifiers())) { throw new MappingException(String.format( "Invalid lazy DBRef property for %s. Found %s which must not be an array nor a final class.", persistentProperty.getField(), persistentProperty.getActualType())); } } }
/** * Writes the given {@link Map} using the given {@link MongoPersistentProperty} information. * * @param map must not {@literal null}. * @param property must not be {@literal null}. * @return */ protected Bson createMap(Map<Object, Object> map, MongoPersistentProperty property) { Assert.notNull(map, "Given map must not be null!"); Assert.notNull(property, "PersistentProperty must not be null!"); if (!property.isDbReference()) { return writeMapInternal(map, new Document(), property.getTypeInformation()); } Document document = new Document(); for (Map.Entry<Object, Object> entry : map.entrySet()) { Object key = entry.getKey(); Object value = entry.getValue(); if (conversions.isSimpleType(key.getClass())) { String simpleKey = prepareMapKey(key.toString()); document.put(simpleKey, value != null ? createDBRef(value, property) : null); } else { throw new MappingException("Cannot use a complex object as a key value."); } } return document; }
@Nullable @SuppressWarnings("unchecked") public <T> T getPropertyValue(MongoPersistentProperty property) { if (property.isDbReference() && property.getDBRef().lazy()) { Object rawRefValue = accessor.get(property); if (rawRefValue == null) { return null; } DbRefResolverCallback callback = new DefaultDbRefResolverCallback(accessor.getDocument(), path, evaluator, MappingMongoConverter.this); DBRef dbref = rawRefValue instanceof DBRef ? (DBRef) rawRefValue : null; return (T) dbRefResolver.resolveDbRef(property, dbref, callback, dbRefProxyHandler); } return super.getPropertyValue(property); } }
if (prop.isDbReference()) {
/** * Writes the given {@link Map} using the given {@link MongoPersistentProperty} information. * * @param map must not {@literal null}. * @param property must not be {@literal null}. * @return */ protected Bson createMap(Map<Object, Object> map, MongoPersistentProperty property) { Assert.notNull(map, "Given map must not be null!"); Assert.notNull(property, "PersistentProperty must not be null!"); if (!property.isDbReference()) { return writeMapInternal(map, new Document(), property.getTypeInformation()); } Document document = new Document(); for (Map.Entry<Object, Object> entry : map.entrySet()) { Object key = entry.getKey(); Object value = entry.getValue(); if (conversions.isSimpleType(key.getClass())) { String simpleKey = prepareMapKey(key.toString()); document.put(simpleKey, value != null ? createDBRef(value, property) : null); } else { throw new MappingException("Cannot use a complex object as a key value."); } } return document; }
/** * Writes the given {@link Collection} using the given {@link MongoPersistentProperty} information. * * @param collection must not be {@literal null}. * @param property must not be {@literal null}. * @return */ protected List<Object> createCollection(Collection<?> collection, MongoPersistentProperty property) { if (!property.isDbReference()) { return writeCollectionInternal(collection, property.getTypeInformation(), new BasicDBList()); } List<Object> dbList = new ArrayList<>(collection.size()); for (Object element : collection) { if (element == null) { continue; } DBRef dbRef = createDBRef(element, property); dbList.add(dbRef); } return dbList; }
private static void potentiallyAssertDBRefTargetType(MongoPersistentProperty persistentProperty) { if (persistentProperty.isDbReference() && persistentProperty.getDBRef().lazy()) { if (persistentProperty.isArray() || Modifier.isFinal(persistentProperty.getActualType().getModifiers())) { throw new MappingException(String.format( "Invalid lazy DBRef property for %s. Found %s which must not be an array nor a final class.", persistentProperty.getField(), persistentProperty.getActualType())); } } }
@Nullable @SuppressWarnings("unchecked") public <T> T getPropertyValue(MongoPersistentProperty property) { if (property.isDbReference() && property.getDBRef().lazy()) { Object rawRefValue = accessor.get(property); if (rawRefValue == null) { return null; } DbRefResolverCallback callback = new DefaultDbRefResolverCallback(accessor.getDocument(), path, evaluator, MappingMongoConverter.this); DBRef dbref = rawRefValue instanceof DBRef ? (DBRef) rawRefValue : null; return (T) dbRefResolver.resolveDbRef(property, dbref, callback, dbRefProxyHandler); } return super.getPropertyValue(property); } }