arangodb / arangodb-java-driver

The official ArangoDB Java driver.
Apache License 2.0
200 stars 93 forks source link

returnNew not working with ArangoCollectionAsync.insertDocuments method #550

Closed superwondercoder closed 3 months ago

superwondercoder commented 3 months ago

Hi, I'm working on a project and I'm trying to insert multiple documents at once but it's also possible some insertions may fail (duplicate and unique index constraints applied). So I was using the following approach:

ArangoCollectionAsync mycol;

...

mycol.insertDocuments(docs, new DocumentCreateOptions().overwriteMode(OverwriteMode.ignore).returnNew(true));

This would return a result asynchronously:

MultiDocumentEntity<DocumentCreateEntity> res

Then I would iterate through the MultiDocumentEntity and recover the created documents with the doc.getNew() method:

List ouput = new ArrayList<>(); res.getDocuments().forEach(doc -> output.add(doc.getNew()));

It worked with the driver version 6.19.0 but once I upgraded to the latest version (7.5.1) it stopped working.

Is this normal? If so, what would be the new way to do so?

Thanks for your feedback

rashtao commented 3 months ago

You are invoking com.arangodb.ArangoCollectionAsync#insertDocuments(java.lang.Iterable<?>, com.arangodb.model.DocumentCreateOptions) which returns CompletableFuture<MultiDocumentEntity<DocumentCreateEntity<Void>>>, so all documents are deserialized as null. You should instead invoke the overloaded variant of this: com.arangodb.ArangoCollectionAsync#insertDocuments(java.lang.Iterable<? extends T>, com.arangodb.model.DocumentCreateOptions, java.lang.Class<T>) which allows specyfying the deserialization target type and returns CompletableFuture<MultiDocumentEntity<DocumentCreateEntity<T>>>.

For example:

CompletableFuture<MultiDocumentEntity<DocumentCreateEntity<BaseDocument>>> res = collection.insertDocuments(docs, new DocumentCreateOptions()
                .overwriteMode(OverwriteMode.ignore)
                .returnNew(true),
                BaseDocument.class);
res.get().getDocuments().forEach(doc -> 
    // use doc.getNew()
);    
superwondercoder commented 3 months ago

Thank you for your fast reply, that actually solved it thanks!