@Override @SuppressWarnings("unchecked") public Class<ID> getIdType() { if (this.entityMetadata.hasIdProperty()) { return super.getIdType(); } return fallbackIdType; } }
/** * Returns the raw identifier for the given {@link MongoPersistentEntity} or the value of the default identifier * field. * * @param entity must not be {@literal null}. * @return */ public Object getRawId(MongoPersistentEntity<?> entity) { return entity.hasIdProperty() ? get(entity.getRequiredIdProperty()) : BsonUtils.asMap(document).get("_id"); }
private void assertUpdateableIdIfNotSet(Object value) { MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(value.getClass()); if (entity != null && entity.hasIdProperty()) { MongoPersistentProperty property = entity.getRequiredIdProperty(); Object propertyValue = entity.getPropertyAccessor(value).getProperty(property); if (propertyValue != null) { return; } if (!MongoSimpleTypes.AUTOGENERATED_ID_TYPES.contains(property.getType())) { throw new InvalidDataAccessApiUsageException( String.format("Cannot autogenerate id of type %s for entity of type %s!", property.getType().getName(), value.getClass().getName())); } } }
public void assertUpdateableIdIfNotSet() { if (!entity.hasIdProperty()) { return; } MongoPersistentProperty property = entity.getRequiredIdProperty(); Object propertyValue = idAccessor.getIdentifier(); if (propertyValue != null) { return; } if (!MongoSimpleTypes.AUTOGENERATED_ID_TYPES.contains(property.getType())) { throw new InvalidDataAccessApiUsageException( String.format("Cannot autogenerate id of type %s for entity of type %s!", property.getType().getName(), entity.getType().getName())); } }
/** * Returns whether the given {@link Field} represents an association reference that together with the given value * requires conversion to a {@link org.springframework.data.mongodb.core.mapping.DBRef} object. We check whether the * type of the given value is compatible with the type of the given document field in order to deal with potential * query field exclusions, since MongoDB uses the {@code int} {@literal 0} as an indicator for an excluded field. * * @param documentField must not be {@literal null}. * @param value * @return */ protected boolean isAssociationConversionNecessary(Field documentField, @Nullable Object value) { Assert.notNull(documentField, "Document field must not be null!"); if (value == null) { return false; } if (!documentField.isAssociation()) { return false; } Class<? extends Object> type = value.getClass(); MongoPersistentProperty property = documentField.getProperty(); if (property.getActualType().isAssignableFrom(type)) { return true; } MongoPersistentEntity<?> entity = documentField.getPropertyEntity(); return entity.hasIdProperty() && (type.equals(DBRef.class) || entity.getRequiredIdProperty().getActualType().isAssignableFrom(type)); }
@Override public Query getByIdQuery() { if (!entity.hasIdProperty()) { throw new MappingException("No id property found for object of type " + entity.getType() + "!"); } MongoPersistentProperty idProperty = entity.getRequiredIdProperty(); return Query.query(Criteria.where(idProperty.getName()).is(getId())); }
/** * Reads the identifier from either the bean backing the {@link PersistentPropertyAccessor} or the source document in * case the identifier has not be populated yet. In this case the identifier is set on the bean for further reference. * * @param accessor must not be {@literal null}. * @param document must not be {@literal null}. * @param entity must not be {@literal null}. * @param path * @param evaluator * @return */ private Object readAndPopulateIdentifier(PersistentPropertyAccessor<?> accessor, DocumentAccessor document, MongoPersistentEntity<?> entity, ObjectPath path, SpELExpressionEvaluator evaluator) { Object rawId = document.getRawId(entity); if (!entity.hasIdProperty() || rawId == null) { return rawId; } MongoPersistentProperty idProperty = entity.getRequiredIdProperty(); if (idProperty.isImmutable() && entity.isConstructorArgument(idProperty)) { return rawId; } accessor.setProperty(idProperty, readIdValue(path, evaluator, idProperty, rawId)); return rawId; }
if (entity != null && entity.hasIdProperty()) { id = convertId(id, entity.getIdProperty().getFieldType());
@Override @SuppressWarnings("unchecked") public Class<ID> getIdType() { if (this.entityMetadata.hasIdProperty()) { return super.getIdType(); } return fallbackIdType; } }
/** * Returns the raw identifier for the given {@link MongoPersistentEntity} or the value of the default identifier * field. * * @param entity must not be {@literal null}. * @return */ public Object getRawId(MongoPersistentEntity<?> entity) { return entity.hasIdProperty() ? get(entity.getRequiredIdProperty()) : BsonUtils.asMap(document).get("_id"); }
private void assertUpdateableIdIfNotSet(Object value) { MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(value.getClass()); if (entity != null && entity.hasIdProperty()) { MongoPersistentProperty property = entity.getRequiredIdProperty(); Object propertyValue = entity.getPropertyAccessor(value).getProperty(property); if (propertyValue != null) { return; } if (!MongoSimpleTypes.AUTOGENERATED_ID_TYPES.contains(property.getType())) { throw new InvalidDataAccessApiUsageException( String.format("Cannot autogenerate id of type %s for entity of type %s!", property.getType().getName(), value.getClass().getName())); } } }
public void assertUpdateableIdIfNotSet() { if (!entity.hasIdProperty()) { return; } MongoPersistentProperty property = entity.getRequiredIdProperty(); Object propertyValue = idAccessor.getIdentifier(); if (propertyValue != null) { return; } if (!MongoSimpleTypes.AUTOGENERATED_ID_TYPES.contains(property.getType())) { throw new InvalidDataAccessApiUsageException( String.format("Cannot autogenerate id of type %s for entity of type %s!", property.getType().getName(), entity.getType().getName())); } }
/** * Returns whether the given {@link Field} represents an association reference that together with the given value * requires conversion to a {@link org.springframework.data.mongodb.core.mapping.DBRef} object. We check whether the * type of the given value is compatible with the type of the given document field in order to deal with potential * query field exclusions, since MongoDB uses the {@code int} {@literal 0} as an indicator for an excluded field. * * @param documentField must not be {@literal null}. * @param value * @return */ protected boolean isAssociationConversionNecessary(Field documentField, @Nullable Object value) { Assert.notNull(documentField, "Document field must not be null!"); if (value == null) { return false; } if (!documentField.isAssociation()) { return false; } Class<? extends Object> type = value.getClass(); MongoPersistentProperty property = documentField.getProperty(); if (property.getActualType().isAssignableFrom(type)) { return true; } MongoPersistentEntity<?> entity = documentField.getPropertyEntity(); return entity.hasIdProperty() && (type.equals(DBRef.class) || entity.getRequiredIdProperty().getActualType().isAssignableFrom(type)); }
@Override public Query getByIdQuery() { if (!entity.hasIdProperty()) { throw new MappingException("No id property found for object of type " + entity.getType() + "!"); } MongoPersistentProperty idProperty = entity.getRequiredIdProperty(); return Query.query(Criteria.where(idProperty.getName()).is(getId())); }
/** * Reads the identifier from either the bean backing the {@link PersistentPropertyAccessor} or the source document in * case the identifier has not be populated yet. In this case the identifier is set on the bean for further reference. * * @param accessor must not be {@literal null}. * @param document must not be {@literal null}. * @param entity must not be {@literal null}. * @param path * @param evaluator * @return */ private Object readAndPopulateIdentifier(PersistentPropertyAccessor<?> accessor, DocumentAccessor document, MongoPersistentEntity<?> entity, ObjectPath path, SpELExpressionEvaluator evaluator) { Object rawId = document.getRawId(entity); if (!entity.hasIdProperty() || rawId == null) { return rawId; } MongoPersistentProperty idProperty = entity.getRequiredIdProperty(); if (idProperty.isImmutable() && entity.isConstructorArgument(idProperty)) { return rawId; } accessor.setProperty(idProperty, readIdValue(path, evaluator, idProperty, rawId)); return rawId; }
public boolean hasIdProperty() { return delegate.hasIdProperty(); }