I'm having trouble with the hibernate @OnDelete annotation to generate a database level cascade delete constraint using ant HibernateToolTask on a @OneToMany relationship, and i'm beginning to go square eyed trying to fix this
Using Hibernate Annotations 3.4.0, connecting to an Oracle 11g Database, using the org.hibernate.dialect.Oracle10gDialect.
I've specified a one way relationship from the Parent to the Child as follows
Code:
@OneToMany
@ForeignKey(name = "FK_NAME")
@OnDelete(action = OnDeleteAction.CASCADE)
@JoinColumn(name = "JOIN_COLUMN_ID", nullable = false)
public Set<Children> getChildren() {
return children;
}
This sucesfully procduces an alter table statement adding the casacde delete constraint and when manually inserting and generating data in the db and performing deltes this works. However, upon starting tomcat, and initialising hibernate i get the following error:
only inverse one-to-many associations may use on-delete="cascade":
So, I've tried creating a bi-directional relationship as follows
Code:
@OneToMany( mappedBy = "parent" )
public Set<Children> getChildren() {
return children;
}
and on the Child class
Code:
@ManyToOne
@ForeignKey(name = "FK_NAME")
@OnDelete(action = OnDeleteAction.CASCADE)
@JoinColumn(name = "JOIN_COLUMN_ID", nullable = false)
public Parent getParent(){
return parent;
}
This version doesn't generate the cascade delete on the foreign key, but also doesn't generate the error upon initialising hibernate.
I've even tried putting @OnDelete both relationships, which causes the foreign key constraint to be added and the error to appear.
Is there any way, or something really simple that i'm missing, to get hibernate to generate the cascade constraint when generating the sql statements, but not to throw the MappingException?
I've seen this question come up on the forums a few times without any answer, so i'm hoping there's some really obvious simple solution that i'm just not seeing at the moment.