Hibernate version: Hibernate 3.2cr4 , Hibernate Annotations 3.2cr2, Hsqldb 1.8.0.4
Assume the following code Node.java:
Code:
@Entity
@Table(name="node")
public class Node {
private List<Node> children = new ArrayList<Node>();
private Node parent;
private Map<String, String> props = new HashMap<String, String>();
... only getters below,
@OneToMany(mappedBy="parent", cascade={javax.persistence.CascadeType.ALL})
@Cascade({CascadeType.ALL})
@Fetch(FetchMode.SELECT)
public List<Node> getChildren() {
return children;
}
@ManyToOne
public Node getParent() {
return parent;
}
@CollectionOfElements
@MapKey(columns={@Column(name="keyy", length=50)}) // keyy beacause key is reserved word in SQL
@Column(name = "value", length = 2500)
@Cascade({CascadeType.ALL})
public Map<String, String> getProps() {
return props;
}
}
Node.java is intended to build a tree structure (parent, children). Each element of the tree might have an arbitrary list of key-value pairs (props). Inserting and updating works fine.
When trying to delete the whole Node tables using
Code:
session.createQuery("delete Node").executeUpdate()
an java.sql.SQLException: Integrity constraint violation FK881D06C3F51D9168 table: NODE in statement [delete from node]
The cause for this are the generated DDL statements:
Code:
create table node (id bigint generated by default as identity (start with 1), path varchar(255) not null, locale varchar(10), sort smallint not null, visible bit not null, parent_id bigint, primary key (id), unique (path))
create table node_props (node_id bigint not null, value varchar(2500), keyy varchar(50), primary key (node_id, keyy))
alter table node add constraint FK881D06C3F51D9168 foreign key (parent_id) references node
alter table node_props add constraint FK979BD4F4238E30AF foreign key (node_id) references avs_node
If the "alter table add contraint" would include "on delete cascade" it should work. I've specified the @Cascade annotation, so why is there no "on delete cascade" in DDL?
This behaviour occurs for both, the @OneToMany and the @CollectionOfElements annotation. Any hints?
Thanks in advance,
Stefan