Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If we take a concrete example would be as follows:</p> <p>Map for table1--> <code>COMPANIES</code>:</p> <pre><code> 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&lt;Branch&gt; branches; //GETTERS, SETTERS... } </code></pre> <p>Map for table2--><code>BRANCHES</code></p> <pre><code> 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&lt;Employee&gt; 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 } </code></pre> <p>Map for table3--><code>STAFF</code></p> <pre><code> 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 } </code></pre> <p>I had a "home remedy" solution for this case. I created 2 extra fields to the primary key of each associated object.</p> <pre><code> @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; } } </code></pre> <p>And specified for each field in the same FK on the <code>setBranchSupervice</code> of the objects, I equaled values​​. With this I can add, edit, delete smoothly.</p> <p>But I still have the doubt as it should be (good practice).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload