I am currently trying to add an attachment to a document. Therfore I have build my own helper.
public void createAndAssignAttachment(InputStream inputStream, AttachmentCreationRequest request) {
LOG.debug("Creating attachment: {} for document: {}", request.getFilename(), request.getDocumentReference());
AttachmentHeader attachmentHeader = attachmentService.createAttachment(
inputStream,
request.getFilename(),
request.getDocumentType(),
request.getAttachmentPath(),
request.getAnnotations()
);
AttachmentReference<DocumentReference> attachmentReference =
AttachmentReference.fromDocRef(request.getDocumentReference());
attachmentHeaderService.assignAttachment(attachmentHeader, attachmentReference);
LOG.info("Successfully created and assigned attachment: {} to document: {}",
request.getFilename(), request.getDocumentReference());
}
Header an d Reference are being created and I can see them in the database. But the document and the attachment table are both empty. What am I missing?
We use the database as a content store. I didn’t realize that in this case the attachment table would be empty — why is it even created then?
But that’s not really the main issue. More important is that the object isn’t attached to the document after saving. I’ve since debugged this myself, since this (getA12) seems to be your only documentation. I found that the parameters annotated with NotNull are not used at all, and that in the entire createAssignment flow no assignment actually takes place. Because I haven’t received an answer here, I built that myself.
public void addAttachment(DocumentReference documentReference, String path, AttachmentHeader attachmentHeader) {
Optional<DataServicesDocument> dataServicesDocument = documentService.load(documentReference);
if (dataServicesDocument.isPresent()) {
IDocument iDocument = dataServicesDocument.get().getKernelDocument();
DocumentV2 document = toV2Document(iDocument);
DocumentV2 updatedDocument = document.withFieldValue(path + "/attachment_id",
attachmentHeader.getAttachmentId())
.withFieldValue(path + "/internal_filename",
attachmentHeader.getFilename())
.withFieldValue(path + "/mime_type", attachmentHeader.getMimeType());
IDocument v1Document = toV1Document(updatedDocument);
documentService.update(documentReference, v1Document, Locale.GERMAN);
}
}
Still, I’d really be interested to know how it’s actually supposed to work, and why it’s so complicated to cover such a simple case, and on top of that, why there’s so little documentation about it.
Hi @soeren-round-ridge,
just an answer on the question:
We use the database as a content store. I didn’t realize that in this case the attachment table would be empty — why is it even created then?
The attachments table is a relict and was needed in pre-content store times so it is deprecated for while now. In the upcoming release 2025.06-ext.2 (Data Services 38.1) it will be removed.
Well, I didn’t use a DocumentV2 document, and as you can see in my example, I used DocumentService.update.
I asked about this earlier but might have missed your reply. I’d still be very interested to understand how it’s supposed to work, since it seems overly complex for such a simple case and it feels somewhat “hacky” to do all that by myself.
Hi @soeren-round-ridge
If you ask about update document, you can use partial update example:
documentService.update(
documentReference,
List.of(
DocumentPart.builder()
.path(path + "/attachment_id")
.value(attachmentHeader.getAttachmentId())
.repetitions(new int[] { 1, 1 }),
DocumentPart.builder()
.path(path + "/internal_filename")
.value(attachmentHeader.getFilename())
.repetitions(new int[] { 1, 1 })
),
Locale.GERMAN
)
Hello @soeren-round-ridge , I am a member of the Discourse team. I can see that there was a new response to your question on the Topic of the discussion. Do you find it helpful, or should the question remain open?
Thanks ahead for your feedback and have a nice rest of the day!