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;
}
};
}
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;
}
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);
}
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;
}
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);
}
Aggregations