@Override public boolean isIdField() { MongoPersistentProperty idProperty = (property != null && property.isIdProperty()) ? property : entity.getIdProperty(); if (idProperty != null) { return name.equals(idProperty.getName()) || name.equals(idProperty.getFieldName()) || name.endsWith("." + idProperty.getName()) || name.endsWith("." + idProperty.getFieldName()); } return DEFAULT_ID_NAMES.contains(name); }
/** * @param entity * @param key * @param mappingContext * @return */ protected Field createPropertyField(@Nullable MongoPersistentEntity<?> entity, String key, MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) { if (entity == null) { return new Field(key); } if (Field.ID_KEY.equals(key)) { return new MetadataBackedField(key, entity, mappingContext, entity.getIdProperty()); } return new MetadataBackedField(key, entity, mappingContext); }
@Nullable @Override public T populateIdIfNecessary(@Nullable Object id) { if (id == null) { return null; } T bean = propertyAccessor.getBean(); MongoPersistentProperty idProperty = entity.getIdProperty(); if (idProperty == null) { return bean; } if (identifierAccessor.getIdentifier() != null) { return bean; } propertyAccessor.setProperty(idProperty, id); return propertyAccessor.getBean(); }
/** * Returns the name of the identifier property. Considers mapping information but falls back to the MongoDB default of * {@code _id} if no identifier property can be found. * * @param type must not be {@literal null}. * @return */ public String getIdPropertyName(Class<?> type) { Assert.notNull(type, "Type must not be null!"); MongoPersistentEntity<?> persistentEntity = context.getPersistentEntity(type); if (persistentEntity != null && persistentEntity.getIdProperty() != null) { return persistentEntity.getRequiredIdProperty().getName(); } return ID_FIELD; }
protected void writeInternal(@Nullable Object obj, Bson bson, @Nullable MongoPersistentEntity<?> entity) { if (obj == null) { return; } if (null == entity) { throw new MappingException("No mapping metadata found for entity of type " + obj.getClass().getName()); } PersistentPropertyAccessor<?> accessor = entity.getPropertyAccessor(obj); DocumentAccessor dbObjectAccessor = new DocumentAccessor(bson); MongoPersistentProperty idProperty = entity.getIdProperty(); if (idProperty != null && !dbObjectAccessor.hasValue(idProperty)) { Object value = idMapper.convertId(accessor.getProperty(idProperty), idProperty.getFieldType()); if (value != null) { dbObjectAccessor.put(idProperty, value); } } writeProperties(bson, entity, accessor, dbObjectAccessor, idProperty); }
protected DBRef createDBRef(Object target, MongoPersistentProperty property) { Assert.notNull(target, "Target object must not be null!"); if (target instanceof DBRef) { return (DBRef) target; } MongoPersistentEntity<?> targetEntity = mappingContext.getPersistentEntity(target.getClass()); targetEntity = targetEntity != null ? targetEntity : mappingContext.getPersistentEntity(property); if (null == targetEntity) { throw new MappingException("No mapping metadata found for " + target.getClass()); } MongoPersistentEntity<?> entity = targetEntity; MongoPersistentProperty idProperty = entity.getIdProperty(); if (idProperty != null) { Object id = target.getClass().equals(idProperty.getType()) ? target : entity.getPropertyAccessor(target).getProperty(idProperty); if (null == id) { throw new MappingException("Cannot create a reference to an object with a NULL id."); } return dbRefResolver.createDbRef(property == null ? null : property.getDBRef(), entity, idMapper.convertId(id, idProperty != null ? idProperty.getFieldType() : ObjectId.class)); } throw new MappingException("No id property found on class " + entity.getType()); }
/** * Returns the given {@link Example} as {@link Document} holding matching values extracted from * {@link Example#getProbe()}. * * @param example must not be {@literal null}. * @param entity must not be {@literal null}. * @return */ public Document getMappedExample(Example<?> example, MongoPersistentEntity<?> entity) { Assert.notNull(example, "Example must not be null!"); Assert.notNull(entity, "MongoPersistentEntity must not be null!"); Document reference = (Document) converter.convertToMongoType(example.getProbe()); if (entity.getIdProperty() != null && ClassUtils.isAssignable(entity.getType(), example.getProbeType())) { Object identifier = entity.getIdentifierAccessor(example.getProbe()).getIdentifier(); if (identifier == null) { reference.remove(entity.getIdProperty().getFieldName()); } } ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor(example.getMatcher()); applyPropertySpecs("", reference, example.getProbeType(), matcherAccessor); Document flattened = ObjectUtils.nullSafeEquals(NullHandler.INCLUDE, matcherAccessor.getNullHandler()) ? reference : new Document(SerializationUtils.flattenMap(reference)); Document result = example.getMatcher().isAllMatching() ? flattened : orConcatenate(flattened); return updateTypeRestrictions(result, example); }
id = convertId(id, entity.getIdProperty().getFieldType());
@Override public boolean isIdField() { MongoPersistentProperty idProperty = (property != null && property.isIdProperty()) ? property : entity.getIdProperty(); if (idProperty != null) { return name.equals(idProperty.getName()) || name.equals(idProperty.getFieldName()) || name.endsWith("." + idProperty.getName()) || name.endsWith("." + idProperty.getFieldName()); } return DEFAULT_ID_NAMES.contains(name); }
/** * Returns the name of the identifier property. Considers mapping information but falls back to the MongoDB default of * {@code _id} if no identifier property can be found. * * @param type must not be {@literal null}. * @return */ public String getIdPropertyName(Class<?> type) { Assert.notNull(type, "Type must not be null!"); MongoPersistentEntity<?> persistentEntity = context.getPersistentEntity(type); if (persistentEntity != null && persistentEntity.getIdProperty() != null) { return persistentEntity.getRequiredIdProperty().getName(); } return ID_FIELD; }
@Nullable @Override public T populateIdIfNecessary(@Nullable Object id) { if (id == null) { return null; } T bean = propertyAccessor.getBean(); MongoPersistentProperty idProperty = entity.getIdProperty(); if (idProperty == null) { return bean; } if (identifierAccessor.getIdentifier() != null) { return bean; } propertyAccessor.setProperty(idProperty, id); return propertyAccessor.getBean(); }
protected void writeInternal(@Nullable Object obj, Bson bson, @Nullable MongoPersistentEntity<?> entity) { if (obj == null) { return; } if (null == entity) { throw new MappingException("No mapping metadata found for entity of type " + obj.getClass().getName()); } PersistentPropertyAccessor<?> accessor = entity.getPropertyAccessor(obj); DocumentAccessor dbObjectAccessor = new DocumentAccessor(bson); MongoPersistentProperty idProperty = entity.getIdProperty(); if (idProperty != null && !dbObjectAccessor.hasValue(idProperty)) { Object value = idMapper.convertId(accessor.getProperty(idProperty)); if (value != null) { dbObjectAccessor.put(idProperty, value); } } writeProperties(bson, entity, accessor, dbObjectAccessor, idProperty); }
protected DBRef createDBRef(Object target, MongoPersistentProperty property) { Assert.notNull(target, "Target object must not be null!"); if (target instanceof DBRef) { return (DBRef) target; } MongoPersistentEntity<?> targetEntity = mappingContext.getPersistentEntity(target.getClass()); targetEntity = targetEntity != null ? targetEntity : mappingContext.getPersistentEntity(property); if (null == targetEntity) { throw new MappingException("No mapping metadata found for " + target.getClass()); } MongoPersistentEntity<?> entity = targetEntity; MongoPersistentProperty idProperty = entity.getIdProperty(); if (idProperty != null) { Object id = target.getClass().equals(idProperty.getType()) ? target : entity.getPropertyAccessor(target).getProperty(idProperty); if (null == id) { throw new MappingException("Cannot create a reference to an object with a NULL id."); } return dbRefResolver.createDbRef(property == null ? null : property.getDBRef(), entity, idMapper.convertId(id)); } throw new MappingException("No id property found on class " + entity.getType()); }
/** * Returns the given {@link Example} as {@link Document} holding matching values extracted from * {@link Example#getProbe()}. * * @param example must not be {@literal null}. * @param entity must not be {@literal null}. * @return */ public Document getMappedExample(Example<?> example, MongoPersistentEntity<?> entity) { Assert.notNull(example, "Example must not be null!"); Assert.notNull(entity, "MongoPersistentEntity must not be null!"); Document reference = (Document) converter.convertToMongoType(example.getProbe()); if (entity.getIdProperty() != null && ClassUtils.isAssignable(entity.getType(), example.getProbeType())) { Object identifier = entity.getIdentifierAccessor(example.getProbe()).getIdentifier(); if (identifier == null) { reference.remove(entity.getIdProperty().getFieldName()); } } ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor(example.getMatcher()); applyPropertySpecs("", reference, example.getProbeType(), matcherAccessor); Document flattened = ObjectUtils.nullSafeEquals(NullHandler.INCLUDE, matcherAccessor.getNullHandler()) ? reference : new Document(SerializationUtils.flattenMap(reference)); Document result = example.getMatcher().isAllMatching() ? flattened : orConcatenate(flattened); return updateTypeRestrictions(result, example); }
public MongoPersistentProperty getIdProperty() { return delegate.getIdProperty(); }
/** * Extract the value of the primary ID of the entity object * * @param entity * @return */ public X getPrimaryID(T entity) { if(mappingContext == null || conversionService == null) { fetchMappingContextAndConversionService(); } MongoPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(entity.getClass()); MongoPersistentProperty idProperty = persistentEntity.getIdProperty(); if(idProperty == null) { return null; } X idValue = BeanWrapper.create(entity, conversionService).getProperty(idProperty, this.primaryIDClass, true); return idValue; }
/** * Extract the value of the primary ID of the entity object * * @param entity * the entity to get primary key value of * * @return the primary key value */ public X getPrimaryID(T entity) { if(mappingContext == null || conversionService == null) { fetchMappingContextAndConversionService(); } MongoPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(entity.getClass()); MongoPersistentProperty idProperty = persistentEntity.getIdProperty(); if(idProperty == null) { return null; } // X idValue = BeanWrapper.create(entity, conversionService).getProperty(idProperty, this.primaryIDClass); X idValue = (X) this.mappingContext.getPersistentEntity(this.entityClass).getPropertyAccessor(entity).getProperty(idProperty); return idValue; }
/** * Get the basic services from mongo template */ private synchronized void fetchMappingContextAndConversionService() { if(mappingContext == null) { MongoConverter mongoConverter = this.mongoTemplate.getConverter(); mappingContext = mongoConverter.getMappingContext(); conversionService = mongoConverter.getConversionService(); MongoPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(entityClass); MongoPersistentProperty idProperty = persistentEntity.getIdProperty(); this.idKey = idProperty == null ? "_id" : idProperty.getName(); } }
/** * Get the basic services from mongo template */ private synchronized void fetchMappingContextAndConversionService() { if(mappingContext == null) { MongoConverter mongoConverter = this.mongoTemplate.getConverter(); mappingContext = mongoConverter.getMappingContext(); conversionService = mongoConverter.getConversionService(); MongoPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(entityClass); MongoPersistentProperty idProperty = persistentEntity.getIdProperty(); this.idKey = idProperty == null ? "_id" : idProperty.getName(); } }
@Override public void partialUpdate(T t) { ID id = getEntityInformation().getId(t); if (null == id) { throw new IllegalArgumentException("ID property should not be null"); } Update update = new Update(); final MongoPersistentEntity<?> persistentEntity = mongoOperations .getConverter().getMappingContext().getPersistentEntity(getEntityInformation().getJavaType()); persistentEntity.doWithProperties((PropertyHandler<MongoPersistentProperty>) persistentProperty -> { if (!persistentEntity.isIdProperty(persistentProperty)) { Object value = Accessible.on(t).field(persistentProperty.getField()).getValue(); if (null != value) { update.set(persistentProperty.getFieldName(), value); } } }); WriteResult writeResult = mongoOperations .updateFirst(query(where(persistentEntity.getIdProperty().getFieldName()).is(id)), update, getEntityInformation().getCollectionName()); if (1 != writeResult.getN()) { throw new IncorrectResultSizeDataAccessException(1, writeResult.getN()); } }