If we take a concrete example would be as follows:
Map for table1--> COMPANIES
:
import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
@Entity
@Table(name = "COMPANIES")
public class Company implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 4)
@Column(name = "COMP_KEY")
private String key;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 100)
@Column(name = "COMP_DESCRIPTION")
private String description;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "company", fetch = FetchType.LAZY)
private List<Branch> branches;
//GETTERS, SETTERS...
}
Map for table2-->BRANCHES
import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
@Entity
@Table(name = "BRANCHES")
public class Branch implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected BranchPK branchPK;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 100)
@Column(name = "BRAN_DESCRIPTION")
private String description;
@JoinColumn(name = "BRAN_COMP_KEY", referencedColumnName = "COMP_KEY")
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private Company company;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "branch", fetch = FetchType.LAZY)
private List<Employee> staff;
//GETTERS, SETTERS, ETC
}
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
@Embeddable
public class BranchPK implements Serializable {
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 4)
@Column(name = "BRAN_COMP_KEY")
private String companyKey;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 4)
@Column(name = "BRAN_KEY")
private String brachKey;
//GETTERS, SETTERS, ETC
}
Map for table3-->STAFF
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
@Entity
@Table(name = "STAFF")
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected EmployeePK employeePK;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 15)
@Column(name = "EMPL_NAME")
private String name;
@JoinColumns({
@JoinColumn(name = "EMPL_COMP_KEY", referencedColumnName = "BRAN_COMP_KEY", insertable = false, updatable = false),
@JoinColumn(name = "EMPL_BRAN_KEY", referencedColumnName = "BRAN_KEY", insertable = false, updatable = false)})
@ManyToOne(optional = false, fetch = FetchType.EAGER)
private Branch branch;
@JoinColumns({
@JoinColumn(name = "EMPL_COMP_KEY", referencedColumnName = "BRAN_COMP_KEY", insertable = false, updatable = false),
@JoinColumn(name = "EMPL_BRAN_SUPER", referencedColumnName = "BRAN_KEY", insertable = false, updatable = false)})
@OneToOne(optional = false)
private Branch branchSupervice;
@JoinColumns({
@JoinColumn(name = "EMPL_COMP_KEY", referencedColumnName = "BRAN_COMP_KEY", insertable = false, updatable = false),
@JoinColumn(name = "EMPL_BRAN_VISIT", referencedColumnName = "BRAN_KEY", insertable = false, updatable = false)})
@OneToOne
private Branch branchVisitor;
//GETTERS, SETTERS, ETC
}
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
@Embeddable
public class EmployeePK implements Serializable {
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 4)
@Column(name = "EMPL_COMP_KEY")
private String companyKey;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 4)
@Column(name = "EMPL_BRAN_KEY")
private String branchKey;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 4)
@Column(name = "EMPL_KEY")
private String employeeKey;
//GETTERS, SETTERS, ETC
}
I had a "home remedy" solution for this case. I created 2 extra fields to the primary key of each associated object.
@Entity
@Table(name = "STAFF")
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
//Besides the above mentioned code
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 4)
@Column(name = "EMPL_BRAN_SUPER")
private String keyBranchSupervice;
@Size(max = 4)
@Column(name = "EMPL_BRAN_VISIT")
private String keyBranchVisitor;
public void setBranchSupervice(Branch branchSupervice) {
this.keyBranchSupervice = branchSupervice.branchPK.getBrachKey();
this.branchSupervice = branchSupervice;
}
}
And specified for each field in the same FK on the setBranchSupervice
of the objects, I equaled values.
With this I can add, edit, delete smoothly.
But I still have the doubt as it should be (good practice).