Search in sources :

Example 21 with MongoPersistentProperty

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);
    }
}
Also used : ConvertingPropertyAccessor(org.springframework.data.mapping.model.ConvertingPropertyAccessor) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)

Example 22 with MongoPersistentProperty

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);
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) Document(org.bson.Document) MappingException(org.springframework.data.mapping.MappingException)

Example 23 with MongoPersistentProperty

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()));
        }
    }
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException)

Example 24 with MongoPersistentProperty

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;
        });
    });
}
Also used : ConvertingPropertyAccessor(org.springframework.data.mapping.model.ConvertingPropertyAccessor) NearQuery(org.springframework.data.mongodb.core.query.NearQuery) Query(org.springframework.data.mongodb.core.query.Query) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) Document(org.bson.Document) FullDocument(com.mongodb.client.model.changestream.FullDocument) ReturnDocument(com.mongodb.client.model.ReturnDocument) Update(org.springframework.data.mongodb.core.query.Update)

Example 25 with MongoPersistentProperty

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);
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) Document(org.bson.Document) FullDocument(com.mongodb.client.model.changestream.FullDocument) ReturnDocument(com.mongodb.client.model.ReturnDocument) MappingException(org.springframework.data.mapping.MappingException)

Aggregations

MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)40 Document (org.bson.Document)13 DBRef (com.mongodb.DBRef)10 Test (org.junit.Test)9 MappingException (org.springframework.data.mapping.MappingException)6 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)6 ConvertingPropertyAccessor (org.springframework.data.mapping.model.ConvertingPropertyAccessor)6 MongoPersistentEntity (org.springframework.data.mongodb.core.mapping.MongoPersistentEntity)6 BasicDBObject (com.mongodb.BasicDBObject)5 DBObject (com.mongodb.DBObject)4 ReturnDocument (com.mongodb.client.model.ReturnDocument)4 FullDocument (com.mongodb.client.model.changestream.FullDocument)4 Arrays (java.util.Arrays)3 Iterator (java.util.Iterator)3 ObjectId (org.bson.types.ObjectId)3 InvalidDataAccessApiUsageException (org.springframework.dao.InvalidDataAccessApiUsageException)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 List (java.util.List)2