I need help for this case.
I have the following entities (I removed getters/setters/hash/toString for easy reading):
@Entity
public class Company implements Serializable{
@Id
private String id;
}
@Entity
public class Document implements Serializable{
@Id
private String id;
}
@Entity
@IdClass(Inbox.PK.class)
public class Inbox implements Serializable {
@Id
@ManyToOne(fetch = FetchType.LAZY)
private Company company;
@Id
@ManyToOne(fetch = FetchType.LAZY)
private Document document;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "inbox")
private Invoice invoice;
public class PK implements Serializable{
private Company company;
private Document document;
}
}
First question is, should I use Company and Document types in PK class or String and String?
And here ... the headache :
@Entity
@IdClass(Invoice.PK.class)
public class Invoice implements Serializable {
@Id
@OneToOne(fetch = FetchType.LAZY, mappedBy = "invoice")
// @MapsId // ???
@JoinColumn(name = "companyId")//, referencedColumnName = "company")// ???
@JoinColumn(name = "documentId")//, referencedColumnName = "document")// ???
// @PrimaryKeyJoinColumn // ????
private Inbox inbox;
@Data
public static class PK implements Serializable {
// private Inbox inbox; // ???
// private String company,document; // ???
// private String companyId,documentId; // ???
// private String inboxCompanyId,inboxDocumentId; // ???
}
}
The PK of the Invoice Entity is also the FK to Inbox (I would like constraints to be generated), and the PK of Inbox is composed of two Entities (Company and Document).
I prefer to use IdClass rather EmbeddedId.
How could I configure Invoice to have, at the end, (company_id,document_id) as PK AND FK to Inbox?
I saw your question posted in upwork. I think you should use string + string type fields with @Id and @Column annotations in PK class.