use of org.springframework.dao.InvalidDataAccessApiUsageException 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()));
}
}
}
use of org.springframework.dao.InvalidDataAccessApiUsageException 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()));
}
}
}
use of org.springframework.dao.InvalidDataAccessApiUsageException in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplate method doRemove.
protected <T> Mono<DeleteResult> doRemove(String collectionName, Query query, @Nullable Class<T> entityClass) {
if (query == null) {
throw new InvalidDataAccessApiUsageException("Query passed in to remove can't be null!");
}
Assert.hasText(collectionName, "Collection name must not be null or empty!");
final Document queryObject = query.getQueryObject();
final MongoPersistentEntity<?> entity = getPersistentEntity(entityClass);
return execute(collectionName, collection -> {
Document removeQuey = queryMapper.getMappedObject(queryObject, entity);
maybeEmitEvent(new BeforeDeleteEvent<T>(removeQuey, entityClass, collectionName));
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.REMOVE, collectionName, entityClass, null, removeQuey);
final DeleteOptions deleteOptions = new DeleteOptions();
query.getCollation().map(Collation::toMongoCollation).ifPresent(deleteOptions::collation);
WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
MongoCollection<Document> collectionToUse = prepareCollection(collection, writeConcernToUse);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Remove using query: {} in collection: {}.", new Object[] { serializeToJsonSafely(removeQuey), collectionName });
}
if (query.getLimit() > 0 || query.getSkip() > 0) {
FindPublisher<Document> cursor = new QueryFindPublisherPreparer(query, entityClass).prepare(//
collection.find(removeQuey)).projection(new Document(ID_FIELD, 1));
return //
Flux.from(cursor).map(//
doc -> doc.get(ID_FIELD)).collectList().flatMapMany(val -> {
return collectionToUse.deleteMany(new Document(ID_FIELD, new Document("$in", val)), deleteOptions);
});
} else {
return collectionToUse.deleteMany(removeQuey, deleteOptions);
}
}).doOnNext(deleteResult -> maybeEmitEvent(new AfterDeleteEvent<T>(queryObject, entityClass, collectionName))).next();
}
use of org.springframework.dao.InvalidDataAccessApiUsageException in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplate method geoNear.
protected <T> Flux<GeoResult<T>> geoNear(NearQuery near, Class<?> entityClass, String collectionName, Class<T> returnType) {
if (near == null) {
throw new InvalidDataAccessApiUsageException("NearQuery must not be null!");
}
if (entityClass == null) {
throw new InvalidDataAccessApiUsageException("Entity class must not be null!");
}
String collection = StringUtils.hasText(collectionName) ? collectionName : determineCollectionName(entityClass);
Document nearDbObject = near.toDocument();
Document command = new Document("geoNear", collection);
command.putAll(nearDbObject);
return Flux.defer(() -> {
if (nearDbObject.containsKey("query")) {
Document query = (Document) nearDbObject.get("query");
command.put("query", queryMapper.getMappedObject(query, getPersistentEntity(entityClass)));
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Executing geoNear using: {} for class: {} in collection: {}", serializeToJsonSafely(command), entityClass, collectionName);
}
GeoNearResultDbObjectCallback<T> callback = new GeoNearResultDbObjectCallback<T>(new ProjectingReadCallback<>(mongoConverter, entityClass, returnType, collectionName), near.getMetric());
return executeCommand(command, this.readPreference).flatMapMany(document -> {
List<Document> l = document.get("results", List.class);
if (l == null) {
return Flux.empty();
}
return Flux.fromIterable(l);
}).skip(near.getSkip() != null ? near.getSkip() : 0).map(callback::doWith);
});
}
Aggregations