Cascade on delete for Hibernate Inheritance model
Answer a question I have a table defined as this: @Entity @Table @Inheritance(strategy=InheritanceType.JOINED) public class Table implements Serializable { @Id @GeneratedValue private Long id; ... } T
Answer a question
I have a table defined as this:
@Entity
@Table
@Inheritance(strategy=InheritanceType.JOINED)
public class Table implements Serializable {
@Id
@GeneratedValue
private Long id;
...
}
Then I have an inherited table:
@Entity
@Table
public class SubTable extends Table {
...
}
Hibernate correctly creates two table in my Postgres database but it defines on-delete action between the two tables as "NO ACTION".
How can I define in Hibernate, that I want CASCADE action to be defined on delete? For example when I manually delete a row from table Table I want to automatically get the corresponding row in table SubTable to be deleted. When I try to delete a row from Table now, it returns me foreign key constraint violation error.
Answers
Hibernate can take care of the cascade deletes if you always use it to perform the deletes.
So removing a SubTable entity will succeed and it will remove both the subclass table record and the associated base class row as well.
If you want to use SQL level deletes, assuming you are using hbm2ddl (which you shouldn't do since you should use Flyway), then you need to annotate the SubTable
with the @OnDelete annotation:
@Entity
@Table
@OnDelete(action = OnDeleteAction.CASCADE)
public class SubTable extends Table {
...
}
更多推荐
所有评论(0)