Hi guys, I am trying to use the the on-cascade feature for foreign-keys (
http://www.mail-archive.com/hibernate-devel@lists.sourceforge.net/msg03801.html).
I have a parent/children relation and these are my mappings
For the parent
Code:
<class name="Parent">
<id name="id" column="ID" type="long">
<generator class="native" />
</id>
<property name="name" column="name" type="java.lang.String" not-null="true"/>
<list name="children" cascade="all,delete-orphan" lazy="true">
<key column="parent_id" on-delete="cascade"/>
<index column="idx"/>
<one-to-many class="Child"/>
</list>
</class>
For the child
Code:
<class name="Child">
<id name="id" column="ID" type="long">
<generator class="native" />
</id>
<property name="name" column="name" type="java.lang.String" />
<many-to-one name="parent" class="Parent" column="parent_id" update="false" insert="false" />
<!-- <many-to-one name="parent" class="Parent" column="parent_id" update="true" insert="true"/> -->
</class>
When I create the database schema using org.hibernate.tool.hbm2ddl.SchemaExportTask everything works fine and I get the following sql:
Code:
alter table Child drop foreign key FK3E104FC2C543486;
drop table if exists Child;
drop table if exists Parent;
create table Child (ID bigint not null auto_increment, name varchar(255), parent_id bigint, idx integer, primary key (ID)) type=InnoDB;
create table Parent (ID bigint not null auto_increment, name varchar(255) not null, primary key (ID)) type=InnoDB;
alter table Child add index FK3E104FC2C543486 (parent_id), add constraint FK3E104FC2C543486 foreign key (parent_id) references Parent (ID) on delete cascade;
Note:
on delete cascadeHowever when I create the session factory, I get the following exception:
Caused by: org.hibernate.MappingException: only inverse one-to-many associations may use on-delete="cascade": org.rivas.model.Parent.childrenI have looked at the code and, this is what is doing (org.hibernate.mapping.Collection.class):
Code:
if ( getKey().isCascadeDeleteEnabled() && ( !isInverse() || !isOneToMany() ) ) {
throw new MappingException(
"only inverse one-to-many associations may use on-delete=\"cascade\": "
+ getRole() );
}
My question is, is it really necesary to be inverse='true'??
I know all the examples show that, but it doesn't say anything in the documentation, and I can't see how it is required.