Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate and Inheritance (TABLE_PER_CLASS)
    primarykey
    data
    text
    <p>I use Hibernate to persist inherited objects but I got this message when I try to persist objects in database:</p> <pre><code>org.springframework.dao.InvalidDataAccessResourceUsageException: Could not execute JDBC batch update; SQL [update Widget set CONTAINER_ID=? where WIDGET_ID=?]; nested exception is org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update (...) Caused by: java.sql.BatchUpdateException: Table 'schema.widget' doesn't exist </code></pre> <p>Here is the classes I used to generate the table:</p> <pre><code>@Entity @Table(name="CONTAINER") public class Container { (...) private Set&lt;Widget&gt; widgets; @OneToMany(targetEntity = Widget.class) @JoinColumn(name="CONTAINER_ID", nullable=true) public Set&lt;Widget&gt; getWidgets() { return widgets; } public void setWidgets(Set&lt;Widget&gt; widgets) { this.widgets = widgets; } } @Entity @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) public abstract class Widget { private long id; private int position; @Id @GeneratedValue(strategy = GenerationType.TABLE) @Column(name="WIDGET_ID") public long getId() { return id; } public void setId(long id) { this.id = id; } @Column(name="POSITION") public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } } @Entity @Table(name="TEXT_WIDGET") public class TextWidget extends Widget { (...) } @Entity @Table(name="IMAGE_WIDGET") public class ImageWidget extends Widget { (...) } </code></pre> <p>So it means that Hibernate is looking for the table 'widget' but it's not created and that make sense because I choose InheritanceType.TABLE_PER_CLASS option then only concrete classes have a table. In database, I can see container, text_widget and image_widget tables.</p> <p>Then when I try to execute this code and save container, then I got the above error:</p> <pre><code>Set&lt;Widget&gt; widgets = new HashSet&lt;Widget&gt;(); widgets.add(textw1); // instance of TextWidget widgets.add(imgw1); // instance of ImageWidget Container container1 = new Container(); container1.setWidgets(widgets); </code></pre> <p>Thanks for your help!</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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