Reputation: 374
I do have a use case two tables are having composite primary key, one table have relation with another table in database , how to get that relationship in program level , how to declare the bean class
//Composite primary key of the table Project
@Embeddable
@Data
public class ProjectId implements Serializable {
private static final long serialVersionUID = 12233365L;
@Column(name = "PROJECT_CODE")
private Integer pCode;
@Column(name = "BOL")
private String bol;
@Column(name = "BOLS")
private String bols;
@Column(name = "EID")
private Integer eId;
}
//Project class
public class Project implements Serializable {
private static final long serialVersionUID = 748569L;
@EmbeddedId
private ProjectId projectId;
@Column(name = "UPDATED_DATE")
private Date updatedDate;
@Column(name = "CREATED_DATE")
private Date createdDate;
}
//pcode, bol, bols, eId are composite key
// Document table, Here some more interesting realtionshipp between Project,Document is "pcode, bol, bols, eId + did"
@Embeddable
@Data
public class DocumentId implements Serializable {
private static final long serialVersionUID = 458933256L;
@Column(name = "PROJECT_CODE")
private Integer pCode;
@Column(name = "BOL")
private String bol;
@Column(name = "BOLS")
private String bols;
@Column(name = "EID")
private Integer eId;
@Column(name = "DID")
private String dId;
}
//Document table
@Entity
@Table(name = "DOCUMENT")
@Data
public class Documents implements Serializable {
private static final long serialVersionUID = 14578523695L;
@EmbeddedId
private DocumentId documentId;
@Column(name = "JUR")
private String jur;
@Column(name = "DSEQUENCE")
private Integer dSequence;
@Column(name = "UPDATED_DATE")
private Date updatedDate;
@Column(name = "CREATED_DATE")
private Date createdDate;
}
Relationship between two table Document is the child table of Project,
I would like make one to relationship from Project side. From Document to project also fine
how can i map one to one relationship between tables ??
Upvotes: 0
Views: 40
Reputation: 21
I don't have enough reputation to add a comment. But how is this a one-to-one mapping as having the column DID in the Document table proves that you can have multiple documents for the same project by having different DID. I am not sure if I am missing something here.
Upvotes: 0
Reputation: 764
Add this member on the Document class:
@OneToOne
@JoinColumns({@JoinColumn(name = "pCode", referencedColumnName = "PROJECT_CODE"),
@JoinColumn(name = "bol", referencedColumnName = "BOL"),
@JoinColumn(name = "bols", referencedColumnName = "BOLS"),
@JoinColumn(name = "eid", referencedColumnName = "EID")})
@MapsId("projectId")
private Project project;
Upvotes: 1