Search in sources :

Example 1 with MongoPersistentProperty

use of org.springframework.data.mongodb.core.mapping.MongoPersistentProperty in project spring-data-mongodb by spring-projects.

the class MongoOperationsUnitTests method operationsSetUp.

@Before
public final void operationsSetUp() {
    person = new Person("Oliver");
    persons = Arrays.asList(person);
    converter = new AbstractMongoConverter(null) {

        public void write(Object t, Bson bson) {
            ((Document) bson).put("firstName", person.getFirstName());
        }

        @SuppressWarnings("unchecked")
        public <S extends Object> S read(Class<S> clazz, Bson bson) {
            return (S) person;
        }

        public MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> getMappingContext() {
            return null;
        }

        public Object convertToMongoType(Object obj, TypeInformation<?> typeInformation) {
            return null;
        }

        public DBRef toDBRef(Object object, MongoPersistentProperty referingProperty) {
            return null;
        }

        @Override
        public MongoTypeMapper getTypeMapper() {
            return null;
        }
    };
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) DBRef(com.mongodb.DBRef) AbstractMongoConverter(org.springframework.data.mongodb.core.convert.AbstractMongoConverter) Bson(org.bson.conversions.Bson) MappingContext(org.springframework.data.mapping.context.MappingContext) MongoPersistentEntity(org.springframework.data.mongodb.core.mapping.MongoPersistentEntity) MongoTypeMapper(org.springframework.data.mongodb.core.convert.MongoTypeMapper) Before(org.junit.Before)

Example 2 with MongoPersistentProperty

use of org.springframework.data.mongodb.core.mapping.MongoPersistentProperty 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 3 with MongoPersistentProperty

use of org.springframework.data.mongodb.core.mapping.MongoPersistentProperty in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplate method findById.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.mongodb.core.ReactiveMongoOperations#findById(java.lang.Object, java.lang.Class, java.lang.String)
	 */
public <T> Mono<T> findById(Object id, Class<T> entityClass, String collectionName) {
    MongoPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(entityClass);
    MongoPersistentProperty idProperty = persistentEntity != null ? persistentEntity.getIdProperty() : null;
    String idKey = idProperty == null ? ID_FIELD : idProperty.getName();
    return doFindOne(collectionName, new Document(idKey, id), null, entityClass, null);
}
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)

Example 4 with MongoPersistentProperty

use of org.springframework.data.mongodb.core.mapping.MongoPersistentProperty in project spring-data-mongodb by spring-projects.

the class DefaultDbRefProxyHandler method populateId.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.mongodb.core.convert.DbRefProxyHandler#populateId(com.mongodb.DBRef, java.lang.Object)
	 */
@Override
public Object populateId(MongoPersistentProperty property, @Nullable DBRef source, Object proxy) {
    if (source == null) {
        return proxy;
    }
    MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(property);
    MongoPersistentProperty idProperty = entity.getRequiredIdProperty();
    if (idProperty.usePropertyAccess()) {
        return proxy;
    }
    SpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator(proxy, spELContext);
    PersistentPropertyAccessor accessor = entity.getPropertyAccessor(proxy);
    Document object = new Document(idProperty.getFieldName(), source.getId());
    ObjectPath objectPath = ObjectPath.ROOT.push(proxy, entity, null);
    accessor.setProperty(idProperty, resolver.getValueInternal(idProperty, object, evaluator, objectPath));
    return proxy;
}
Also used : DefaultSpELExpressionEvaluator(org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) SpELExpressionEvaluator(org.springframework.data.mapping.model.SpELExpressionEvaluator) DefaultSpELExpressionEvaluator(org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) Document(org.bson.Document)

Example 5 with MongoPersistentProperty

use of org.springframework.data.mongodb.core.mapping.MongoPersistentProperty in project spring-data-mongodb by spring-projects.

the class ConvertingParameterAccessorUnitTests method setupAndConvert.

private Object setupAndConvert(Object... parameters) {
    MongoParameterAccessor delegate = new StubParameterAccessor(parameters);
    PotentiallyConvertingIterator iterator = new ConvertingParameterAccessor(converter, delegate).iterator();
    MongoPersistentEntity<?> entity = context.getRequiredPersistentEntity(Entity.class);
    MongoPersistentProperty property = entity.getRequiredPersistentProperty("property");
    return iterator.nextConverted(property);
}
Also used : PotentiallyConvertingIterator(org.springframework.data.mongodb.repository.query.ConvertingParameterAccessor.PotentiallyConvertingIterator) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)

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