use of org.springframework.data.mongodb.core.mapping.MongoPersistentProperty in project spring-data-mongodb by spring-projects.
the class MongoTemplate method initializeVersionProperty.
private void initializeVersionProperty(Object entity) {
MongoPersistentEntity<?> persistentEntity = getPersistentEntity(entity.getClass());
if (persistentEntity != null && persistentEntity.hasVersionProperty()) {
MongoPersistentProperty versionProperty = persistentEntity.getRequiredVersionProperty();
ConvertingPropertyAccessor accessor = new ConvertingPropertyAccessor(persistentEntity.getPropertyAccessor(entity), mongoConverter.getConversionService());
accessor.setProperty(versionProperty, 0);
}
}
use of org.springframework.data.mongodb.core.mapping.MongoPersistentProperty in project spring-data-mongodb by spring-projects.
the class MongoTemplate method extractIdPropertyAndValue.
/**
* Returns {@link Entry} containing the field name of the id property as {@link Entry#getKey()} and the {@link Id}s
* property value as its {@link Entry#getValue()}.
*
* @param object
* @return
*/
private Pair<String, Object> extractIdPropertyAndValue(Object object) {
Assert.notNull(object, "Id cannot be extracted from 'null'.");
Class<?> objectType = object.getClass();
if (object instanceof Document) {
return Pair.of(ID_FIELD, ((Document) object).get(ID_FIELD));
}
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(objectType);
if (entity != null && entity.hasIdProperty()) {
MongoPersistentProperty idProperty = entity.getIdProperty();
return Pair.of(idProperty.getFieldName(), entity.getPropertyAccessor(object).getProperty(idProperty));
}
throw new MappingException("No id property found for object of type " + objectType);
}
use of org.springframework.data.mongodb.core.mapping.MongoPersistentProperty in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplate method assertUpdateableIdIfNotSet.
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()));
}
}
}
use of org.springframework.data.mongodb.core.mapping.MongoPersistentProperty in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplate method doSaveVersioned.
private <T> Mono<T> doSaveVersioned(T objectToSave, MongoPersistentEntity<?> entity, String collectionName) {
return createMono(collectionName, collection -> {
ConvertingPropertyAccessor convertingAccessor = new ConvertingPropertyAccessor(entity.getPropertyAccessor(objectToSave), mongoConverter.getConversionService());
MongoPersistentProperty idProperty = entity.getRequiredIdProperty();
MongoPersistentProperty versionProperty = entity.getRequiredVersionProperty();
Object version = convertingAccessor.getProperty(versionProperty);
Number versionNumber = convertingAccessor.getProperty(versionProperty, Number.class);
// Fresh instance -> initialize version property
if (version == null) {
return doInsert(collectionName, objectToSave, mongoConverter);
}
assertUpdateableIdIfNotSet(objectToSave);
// Create query for entity with the id and old version
Object id = convertingAccessor.getProperty(idProperty);
Query query = new Query(Criteria.where(idProperty.getName()).is(id).and(versionProperty.getName()).is(version));
if (versionNumber == null) {
versionNumber = 0;
}
// Bump version number
convertingAccessor.setProperty(versionProperty, versionNumber.longValue() + 1);
ReactiveMongoTemplate.this.maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
Document document = ReactiveMongoTemplate.this.toDbObject(objectToSave, mongoConverter);
ReactiveMongoTemplate.this.maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, document, collectionName));
Update update = Update.fromDocument(document, ID_FIELD);
return doUpdate(collectionName, query, update, objectToSave.getClass(), false, false).map(updateResult -> {
maybeEmitEvent(new AfterSaveEvent<T>(objectToSave, document, collectionName));
return objectToSave;
});
});
}
use of org.springframework.data.mongodb.core.mapping.MongoPersistentProperty in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplate method extractIdPropertyAndValue.
/**
* Returns {@link Entry} containing the field name of the id property as {@link Entry#getKey()} and the {@link Id}s
* property value as its {@link Entry#getValue()}.
*
* @param object
* @return
*/
private Pair<String, Object> extractIdPropertyAndValue(Object object) {
Assert.notNull(object, "Id cannot be extracted from 'null'.");
Assert.notNull(object, "Id cannot be extracted from 'null'.");
Class<?> objectType = object.getClass();
if (object instanceof Document) {
return Pair.of(ID_FIELD, ((Document) object).get(ID_FIELD));
}
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(objectType);
if (entity != null && entity.hasIdProperty()) {
MongoPersistentProperty idProperty = entity.getIdProperty();
return Pair.of(idProperty.getFieldName(), entity.getPropertyAccessor(object).getProperty(idProperty));
}
throw new MappingException("No id property found for object of type " + objectType);
}
Aggregations