Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your association must be bidirectional, as mentioned in the Hibernate documentation.</p> <blockquote> <h3><a href="http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#d0e1191" rel="nofollow noreferrer">2.2.4.1. Table per class</a></h3> <p>This strategy has many drawbacks (esp. with polymorphic queries and associations) explained in the JPA spec, the Hibernate reference documentation, Hibernate in Action, and many other places. Hibernate work around most of them implementing this strategy using SQL UNION queries. It is commonly used for the top level of an inheritance hierarchy:</p> <pre><code>@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class Flight implements Serializable { ... } </code></pre> <p>This strategy supports one-to-many associations <strong>provided that they are bidirectional</strong>. This strategy does not support the IDENTITY generator strategy: the id has to be shared across several tables. Consequently, when using this strategy, you should not use AUTO nor IDENTITY.</p> </blockquote> <p>This way, Hibernate will be able to create the appropriate foreign key column in each concrete widget table. Here is a mapping that actually works. For the <code>Container</code>:</p> <pre><code>@Entity public class Container { @Id @GeneratedValue private long id; @OneToMany(targetEntity = Widget.class, mappedBy = "container", cascade = CascadeType.ALL) private Set&lt;Widget&gt; widgets = new HashSet&lt;Widget&gt;(); public long getId() { return id; } public void setId(long id) { this.id = id; } public Set&lt;Widget&gt; getWidgets() { return widgets; } public void setWidgets(Set&lt;Widget&gt; widgets) { this.widgets = widgets; } public void addToWidgets(Widget widget) { this.getWidgets().add(widget); widget.setContainer(this); } public void removeFromWidgets(Widget widget) { this.getWidgets().remove(widget); widget.setContainer(null); } } </code></pre> <p>And the abstract Widget class:</p> <pre><code>@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public abstract class Widget { @Id @GeneratedValue(strategy = GenerationType.TABLE) private long id; private int position; @ManyToOne private Container container; public long getId() { return id; } public void setId(long id) { this.id = id; } public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } public Container getContainer() { return container; } public void setContainer(Container container) { this.container = container; } } </code></pre> <p>And the following test method (running inside a transaction) just passes:</p> <pre><code>@Test public void testInsertContainer() { TextWidget textw1 = new TextWidget(); ImageWidget imgw1 = new ImageWidget(); Container container1 = new Container(); container1.addToWidgets(textw1); // instance of TextWidget container1.addToWidgets(imgw1); // instance of ImageWidget session.persist(container1); session.flush(); assertNotNull(textw1.getId()); assertNotNull(imgw1.getId()); assertNotNull(container1.getId()); } </code></pre> <p>And generates the following SQL:</p> <pre> 21:59:57.964 [main] DEBUG org.hibernate.SQL - select next_hi from hibernate_unique_key for read only with rs 21:59:57.978 [main] DEBUG org.hibernate.SQL - update hibernate_unique_key set next_hi = ? where next_hi = ? 21:59:58.063 [main] DEBUG org.hibernate.SQL - null 21:59:58.125 [main] DEBUG org.hibernate.SQL - insert into Container (id) values (?) Hibernate: insert into Container (id) values (?) 21:59:58.140 [main] TRACE org.hibernate.type.LongType - binding '98304' to parameter: 1 21:59:58.145 [main] DEBUG org.hibernate.SQL - insert into ImageWidget (container_id, position, id) values (?, ?, ?) Hibernate: insert into ImageWidget (container_id, position, id) values (?, ?, ?) 21:59:58.164 [main] TRACE org.hibernate.type.LongType - binding '98304' to parameter: 1 21:59:58.165 [main] TRACE org.hibernate.type.IntegerType - binding '0' to parameter: 2 21:59:58.166 [main] TRACE org.hibernate.type.LongType - binding '32768' to parameter: 3 21:59:58.172 [main] DEBUG org.hibernate.SQL - insert into TextWidget (container_id, position, id) values (?, ?, ?) Hibernate: insert into TextWidget (container_id, position, id) values (?, ?, ?) 21:59:58.187 [main] TRACE org.hibernate.type.LongType - binding '98304' to parameter: 1 21:59:58.188 [main] TRACE org.hibernate.type.IntegerType - binding '0' to parameter: 2 21:59:58.189 [main] TRACE org.hibernate.type.LongType - binding '32769' to parameter: 3 </pre> <p>But keep in mind that the <em>table per class</em> strategy provides <strong>poor support for polymorphic relationships</strong> and might not be appropriate at all if you have many <code>Widget</code> child (this will result in huge SQL UNION).</p> <h3>Related question</h3> <ul> <li><a href="https://stackoverflow.com/questions/2914972/jpa-polymorphic-onetomany/2918513#2918513">JPA polymorphic oneToMany</a></li> </ul>
    singulars
    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. VO
      singulars
      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