Search in sources :

Example 16 with MongoPersistentProperty

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

the class MongoQueryCreator method create.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.repository.query.parser.AbstractQueryCreator#create(org.springframework.data.repository.query.parser.Part, java.util.Iterator)
	 */
@Override
protected Criteria create(Part part, Iterator<Object> iterator) {
    if (isGeoNearQuery && part.getType().equals(Type.NEAR)) {
        return null;
    }
    PersistentPropertyPath<MongoPersistentProperty> path = context.getPersistentPropertyPath(part.getProperty());
    MongoPersistentProperty property = path.getLeafProperty();
    Criteria criteria = from(part, property, where(path.toDotPath()), (PotentiallyConvertingIterator) iterator);
    return criteria;
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) Criteria(org.springframework.data.mongodb.core.query.Criteria)

Example 17 with MongoPersistentProperty

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

the class SpringDataMongodbSerializer method asDBKey.

/*
	 * (non-Javadoc)
	 * @see com.querydsl.mongodb.MongodbSerializer#asDBKey(com.querydsl.core.types.Operation, int)
	 */
@Override
protected String asDBKey(@Nullable Operation<?> expr, int index) {
    Expression<?> arg = expr.getArg(index);
    String key = super.asDBKey(expr, index);
    if (!(arg instanceof Path)) {
        return key;
    }
    Path<?> path = (Path<?>) arg;
    if (!isReference(path)) {
        return key;
    }
    MongoPersistentProperty property = getPropertyFor(path);
    return property.isIdProperty() ? key.replaceAll("." + ID_KEY + "$", "") : key;
}
Also used : Path(com.querydsl.core.types.Path) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)

Example 18 with MongoPersistentProperty

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

the class SpringDataMongodbSerializer method getKeyForPath.

/*
	 * (non-Javadoc)
	 * @see com.querydsl.mongodb.MongodbSerializer#getKeyForPath(com.querydsl.core.types.Path, com.querydsl.core.types.PathMetadata)
	 */
@Override
protected String getKeyForPath(Path<?> expr, PathMetadata metadata) {
    if (!metadata.getPathType().equals(PathType.PROPERTY)) {
        return super.getKeyForPath(expr, metadata);
    }
    Path<?> parent = metadata.getParent();
    MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(parent.getType());
    MongoPersistentProperty property = entity.getPersistentProperty(metadata.getName());
    return property == null ? super.getKeyForPath(expr, metadata) : property.getFieldName();
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)

Example 19 with MongoPersistentProperty

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

the class MongoTemplate 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 20 with MongoPersistentProperty

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

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