We are working on a system in which a person can send a document to another person, the document can have several attachments, as indicated below.
Document {
Set<Attachment> attachments;
}
If X sends the document (Doc1, Ver1) to Y and Y, edits the document, then we must create a new version (Doc1, Ver2) of the document so that the X sent box does not reflect the changes made by Y.
There will be millions of documents in our system in which each document can have hundreds of attachments. Also, a document can travel through n people for a certain period of time.
We should be able to download any version of the document along with a list of attachments that were at that time, so I need to support versions of the document, so the table structure immediately appeared, which was lower.
Document - id primary key, ver_id primary key
Attachment - id, doc_id foreign key, doc_ver_id foreign key
But the problem with this structure is that if X sends the document to Y with 100 attachments, and Y made some minor changes, then I need to create a new version along with copying all the attachments for the new version, most of which will be the same as in the previous version, since we will have millions of documents, and each document will be moved by n number of people. This model will lead to a very large application table with a lot of redundant data.
So, we thought of an alternative structure for an investment, as shown below.
Document - id primary key, ver_id primary key
Attachment - id, doc_id, attached_ver_id, detached_version_id
Hibernate Document , , - , , .