Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The way to do this is to define two @Embeddable entities to represent the composite keys for A and B. BKey is identical to AKey except that it adds "a6" property. </p> <p>AKey Class:</p> <pre><code>@Embeddable public class AKey implements Serializable { private String a1; private String a2; private String a3; private String a4; private String a5; public AKey() { } public AKey(String a1, String a2, String a3, String a4, String a5) { this.a1 = a1; this.a2 = a2; this.a3 = a3; this.a4 = a4; this.a5 = a5; } ... // getters &amp; setters } </code></pre> <p>Then you define your A class to use the Embeddable composite key class as the @IdClass but still use the composite key elements as properties. You would do the same thing for the B class (using BKey). Define the @OneToMany mapping in A for the "Set bs" property with the required @JoinColumns.</p> <pre><code>@Entity @IdClass(AKey.class) public class A { @Id private String a1; private String a2; private String a3; private String a4; private String a5; @OneToMany(targetEntity=B.class, cascade={CascadeType.ALL}) @JoinColumns({@JoinColumn(name="a1"), @JoinColumn(name="a2"), @JoinColumn(name="a3"), @JoinColumn(name="a4"), @JoinColumn(name="a5")}) private Set&lt;B&gt; bs = new HashSet&lt;B&gt;(); public A() { } public A(AKey id, String prop1) { this.a1 = id.getA1(); this.a2 = id.getA2(); this.a3 = id.getA3(); this.a4 = id.getA4(); this.a5 = id.getA5(); this.prop1 = prop1; } public Set&lt;B&gt; getBs() { return bs; } public void setBs(Set&lt;B&gt; bs) { this.bs = bs; } ... // getters &amp; setters and anything else in A } </code></pre> <p>I didn't have any issues with this approach - it expected the table structure you defined (A with a 5-column primary key, B with a 6-column primary key and a 5-column foreign key to A). </p> <p>The following test case correctly created an A with 2 associated Bs that could be retrieved using JQL.</p> <pre><code>B b = new B(new BKey("a", "b", "c", "d", "e", "1"), "prop1"); B b2 = new B(new BKey("a", "b", "c", "d", "e", "2"), "prop1"); A a = new A(new AKey("a", "b", "c", "d", "e"), "prop1"); a.getBs().add(b); a.getBs().add(b2); manager.persist(a); .... </code></pre> <p>Hope this helps.</p> <p>Sarah</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.
    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