-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: on-cascade for foreign-keys, only for inverse='true' ???
PostPosted: Wed Oct 24, 2007 11:20 am 
Newbie

Joined: Mon Apr 30, 2007 12:27 pm
Posts: 18
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 cascade

However 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.children

I 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.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 03, 2008 3:50 pm 
Newbie

Joined: Tue Jun 03, 2008 2:53 pm
Posts: 1
Any solution to this? I have the same problem. I want to be able to bulk delete the parent using a delete HQL query instead of a row at a time. When I try and use the on-delete="cascade", my schema is generated correctly, but Hibernate complains about the mapping. If I hand tweak the db schema for the cascade on-delete but eliminate the mapping on delete, the bulk delete does what is expected. I wanted to drive everything from the mapping file.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.