Attachment.java

package com.tdmconsult.ete.documents;

import com.tdmconsult.ete.utils.AbstractEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.Lob;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import java.util.UUID;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.jspecify.annotations.Nullable;

@Entity
@Data
@Accessors(chain = true)
@Table(name = "ete_financial_document_attachments")
@EqualsAndHashCode(callSuper = true)
public class Attachment extends AbstractEntity {

    public static Attachment of(final FinancialDocument financialDocument) {
        final var document = new Attachment();
        document.setPublicId(UUID.randomUUID().toString());
        document.setFinancialDocument(financialDocument);
        financialDocument.getAttachments().add(document);
        return document;
    }

    @Lob
    @ToString.Exclude
    private byte @Nullable[] fileData;
    @Nullable
    private String fileName;
    private long fileSize;
    @Nullable
    private String fileContentType;
    private String uploadBy;

    @ToString.Exclude
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "financial_document_id", nullable = false)
    private FinancialDocument financialDocument;
}