Search in sources :

Example 1 with ConvertingPropertyAccessor

use of org.springframework.data.mapping.model.ConvertingPropertyAccessor in project spring-data-mongodb by spring-projects.

the class MongoTemplate method doSaveVersioned.

private <T> T doSaveVersioned(T objectToSave, MongoPersistentEntity<?> entity, String collectionName) {
    ConvertingPropertyAccessor convertingAccessor = new ConvertingPropertyAccessor(entity.getPropertyAccessor(objectToSave), mongoConverter.getConversionService());
    MongoPersistentProperty property = entity.getRequiredVersionProperty();
    Number number = convertingAccessor.getProperty(property, Number.class);
    if (number != null) {
        // Bump version number
        convertingAccessor.setProperty(property, number.longValue() + 1);
        maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave, collectionName));
        assertUpdateableIdIfNotSet(objectToSave);
        Document document = new Document();
        this.mongoConverter.write(objectToSave, document);
        maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, document, collectionName));
        Update update = Update.fromDocument(document, ID_FIELD);
        // Create query for entity with the id and old version
        MongoPersistentProperty idProperty = entity.getRequiredIdProperty();
        Object id = entity.getIdentifierAccessor(objectToSave).getRequiredIdentifier();
        Query query = new Query(Criteria.where(idProperty.getName()).is(id).and(property.getName()).is(number));
        UpdateResult result = doUpdate(collectionName, query, update, objectToSave.getClass(), false, false);
        if (result.getModifiedCount() == 0) {
            throw new OptimisticLockingFailureException(String.format("Cannot save entity %s with version %s to collection %s. Has it been modified meanwhile?", id, number, collectionName));
        }
        maybeEmitEvent(new AfterSaveEvent<T>(objectToSave, document, collectionName));
        return objectToSave;
    }
    doInsert(collectionName, objectToSave, this.mongoConverter);
    return objectToSave;
}
Also used : 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) Update(org.springframework.data.mongodb.core.query.Update) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) ConvertingPropertyAccessor(org.springframework.data.mapping.model.ConvertingPropertyAccessor) UpdateResult(com.mongodb.client.result.UpdateResult)

Example 2 with ConvertingPropertyAccessor

use of org.springframework.data.mapping.model.ConvertingPropertyAccessor in project spring-data-mongodb by spring-projects.

the class MappingMongoConverter method read.

@Nullable
private <S extends Object> S read(final MongoPersistentEntity<S> entity, final Document bson, final ObjectPath path) {
    DefaultSpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator(bson, spELContext);
    ParameterValueProvider<MongoPersistentProperty> provider = getParameterProvider(entity, bson, evaluator, path);
    EntityInstantiator instantiator = instantiators.getInstantiatorFor(entity);
    S instance = instantiator.createInstance(entity, provider);
    PersistentPropertyAccessor accessor = new ConvertingPropertyAccessor(entity.getPropertyAccessor(instance), conversionService);
    MongoPersistentProperty idProperty = entity.getIdProperty();
    DocumentAccessor documentAccessor = new DocumentAccessor(bson);
    // make sure id property is set before all other properties
    Object idValue = null;
    if (idProperty != null && documentAccessor.hasValue(idProperty)) {
        idValue = readIdValue(path, evaluator, idProperty, documentAccessor);
        accessor.setProperty(idProperty, idValue);
    }
    ObjectPath currentPath = path.push(instance, entity, idValue != null ? bson.get(idProperty.getFieldName()) : null);
    MongoDbPropertyValueProvider valueProvider = new MongoDbPropertyValueProvider(documentAccessor, evaluator, currentPath);
    DbRefResolverCallback callback = new DefaultDbRefResolverCallback(bson, currentPath, evaluator, MappingMongoConverter.this);
    readProperties(entity, accessor, idProperty, documentAccessor, valueProvider, callback);
    return instance;
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) EntityInstantiator(org.springframework.data.convert.EntityInstantiator) DefaultSpELExpressionEvaluator(org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator) ConvertingPropertyAccessor(org.springframework.data.mapping.model.ConvertingPropertyAccessor) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Nullable(org.springframework.lang.Nullable)

Example 3 with ConvertingPropertyAccessor

use of org.springframework.data.mapping.model.ConvertingPropertyAccessor in project spring-data-jdbc by spring-projects.

the class EntityRowMapper method readEntityFrom.

private <S> S readEntityFrom(ResultSet rs, PersistentProperty<?> property) {
    String prefix = property.getName() + "_";
    @SuppressWarnings("unchecked") JdbcPersistentEntity<S> entity = (JdbcPersistentEntity<S>) context.getRequiredPersistentEntity(property.getActualType());
    if (readFrom(rs, entity.getRequiredIdProperty(), prefix) == null) {
        return null;
    }
    S instance = instantiator.createInstance(entity, new ResultSetParameterValueProvider(rs, entity, conversions, prefix));
    PersistentPropertyAccessor accessor = entity.getPropertyAccessor(instance);
    ConvertingPropertyAccessor propertyAccessor = new ConvertingPropertyAccessor(accessor, conversions);
    for (JdbcPersistentProperty p : entity) {
        propertyAccessor.setProperty(p, readFrom(rs, p, prefix));
    }
    return instance;
}
Also used : JdbcPersistentProperty(org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty) ConvertingPropertyAccessor(org.springframework.data.mapping.model.ConvertingPropertyAccessor) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) JdbcPersistentEntity(org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity)

Example 4 with ConvertingPropertyAccessor

use of org.springframework.data.mapping.model.ConvertingPropertyAccessor in project spring-data-jdbc by spring-projects.

the class EntityRowMapper method mapRow.

/*
	 * (non-Javadoc)
	 * @see org.springframework.jdbc.core.RowMapper#mapRow(java.sql.ResultSet, int)
	 */
@Override
public T mapRow(ResultSet resultSet, int rowNumber) throws SQLException {
    T result = createInstance(resultSet);
    ConvertingPropertyAccessor propertyAccessor = new ConvertingPropertyAccessor(entity.getPropertyAccessor(result), conversions);
    Object id = idProperty == null ? null : readFrom(resultSet, idProperty, "");
    for (JdbcPersistentProperty property : entity) {
        if (property.isCollectionLike()) {
            propertyAccessor.setProperty(property, accessStrategy.findAllByProperty(id, property));
        } else if (property.isMap()) {
            Iterable<Object> allByProperty = accessStrategy.findAllByProperty(id, property);
            propertyAccessor.setProperty(property, ITERABLE_OF_ENTRY_TO_MAP_CONVERTER.convert(allByProperty));
        } else {
            propertyAccessor.setProperty(property, readFrom(resultSet, property, ""));
        }
    }
    return result;
}
Also used : JdbcPersistentProperty(org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty) ConvertingPropertyAccessor(org.springframework.data.mapping.model.ConvertingPropertyAccessor)

Example 5 with ConvertingPropertyAccessor

use of org.springframework.data.mapping.model.ConvertingPropertyAccessor in project spring-data-mongodb by spring-projects.

the class MongoTemplate method populateIdIfNecessary.

/**
 * Populates the id property of the saved object, if it's not set already.
 *
 * @param savedObject
 * @param id
 */
protected void populateIdIfNecessary(Object savedObject, Object id) {
    if (id == null) {
        return;
    }
    if (savedObject instanceof Document) {
        Document document = (Document) savedObject;
        document.put(ID_FIELD, id);
        return;
    }
    MongoPersistentProperty idProperty = getIdPropertyFor(savedObject.getClass());
    if (idProperty != null) {
        ConversionService conversionService = mongoConverter.getConversionService();
        MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(savedObject.getClass());
        PersistentPropertyAccessor accessor = entity.getPropertyAccessor(savedObject);
        Object value = accessor.getProperty(idProperty);
        if (value == null) {
            new ConvertingPropertyAccessor(accessor, conversionService).setProperty(idProperty, id);
        }
    }
}
Also used : ConvertingPropertyAccessor(org.springframework.data.mapping.model.ConvertingPropertyAccessor) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) ConversionService(org.springframework.core.convert.ConversionService) Document(org.bson.Document)

Aggregations

ConvertingPropertyAccessor (org.springframework.data.mapping.model.ConvertingPropertyAccessor)9 MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)6 Document (org.bson.Document)4 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)4 ReturnDocument (com.mongodb.client.model.ReturnDocument)2 FullDocument (com.mongodb.client.model.changestream.FullDocument)2 ConversionService (org.springframework.core.convert.ConversionService)2 JdbcPersistentProperty (org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty)2 NearQuery (org.springframework.data.mongodb.core.query.NearQuery)2 Query (org.springframework.data.mongodb.core.query.Query)2 Update (org.springframework.data.mongodb.core.query.Update)2 BasicDBObject (com.mongodb.BasicDBObject)1 DBObject (com.mongodb.DBObject)1 UpdateResult (com.mongodb.client.result.UpdateResult)1 OptimisticLockingFailureException (org.springframework.dao.OptimisticLockingFailureException)1 EntityInstantiator (org.springframework.data.convert.EntityInstantiator)1 JdbcPersistentEntity (org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity)1 DefaultSpELExpressionEvaluator (org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator)1 Nullable (org.springframework.lang.Nullable)1