Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong><code>@JoinColumn</code> could be used on both sides of the relationship.</strong> The question was about using <code>@JoinColumn</code> on the <code>@OneToMany</code> side (rare case). And the point here is in <em>physical information duplication</em> (column name) along with <em>not optimized SQL query that will produce some additional UPDATE statements</em>.</p> <p>According to <a href="http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/">documentation</a>:</p> <p>Since <strong>many to one are</strong> (almost) always the <strong>owner side</strong> of a bidirectional relationship in the JPA spec, the one to many association is annotated by @OneToMany(mappedBy=...)</p> <pre><code>@Entity public class Troop { @OneToMany(mappedBy="troop") public Set&lt;Soldier&gt; getSoldiers() { ... } @Entity public class Soldier { @ManyToOne @JoinColumn(name="troop_fk") public Troop getTroop() { ... } </code></pre> <p>Troop has a bidirectional one to many relationship with Soldier through the troop property. You don't have to (must not) define any physical mapping in the mappedBy side.</p> <p>To map a bidirectional one to many, with the <strong>one-to-many side as the owning side</strong>, you have to remove the mappedBy element and set the many to one @JoinColumn as insertable and updatable to false. This solution is not optimized and will produce some additional UPDATE statements.</p> <pre><code>@Entity public class Troop { @OneToMany @JoinColumn(name="troop_fk") //we need to duplicate the physical information public Set&lt;Soldier&gt; getSoldiers() { ... } @Entity public class Soldier { @ManyToOne @JoinColumn(name="troop_fk", insertable=false, updatable=false) public Troop getTroop() { ... } </code></pre>
 

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