Hi,
I use Hibernate to persist inherited objects but I got this message when I try to persist objects in database:
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
Here is the classes I used to generate the table:
@Entity
@Table(name="CONTAINER")
public class Container {
(...)
private Set<Widget> widgets;
@OneToMany(targetEntity = Widget.class)
@JoinColumn(name="CONTAINER_ID", nullable=true)
public Set<Widget> getWidgets() {
return widgets;
}
public void setWidgets(Set<Widget> 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 {
(...)
}
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.
Then when I try to execute this code and save container, then I got the above error:
Set<Widget> widgets = new HashSet<Widget>();
widgets.add(textw1); // instance of TextWidget
widgets.add(imgw1); // instance of ImageWidget
Container container1 = new Container();
container1.setWidgets(widgets);
Thanks for your help!