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.  [ 1 post ] 
Author Message
 Post subject: cascading delete, why not one delete on the foreign key ?
PostPosted: Mon Aug 23, 2010 5:36 pm 
Newbie

Joined: Sun Aug 22, 2010 4:16 pm
Posts: 2
I'm wondering why hibernate generates 1 delete per entity on a child table
instead of using one delete on the foreign key

Here's the hibernate.cfg.xml for this toy project
Code:
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.url">jdbc:hsqldb:file:testdb;shutdown=true</property>
        <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
        <property name="hibernate.connection.username">sa</property>
        <property name="hibernate.connection.password"></property>
        <property name="hibernate.connection.pool_size">0</property>
        <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hbm2ddl.auto">auto</property>
        <mapping file="entities/Question.hbm.xml"/>
        <mapping file="entities/Answer.hbm.xml"/>
   
    </session-factory>


Question.hbm.xml

Code:
<hibernate-mapping>
   <class name="entities.Question">
      <id name="id">
         <generator class="native" />
      </id>
      <property name="title" not-null="true">
      </property>

      <property name="question" type="text" not-null="true">
      </property>

      <bag name="answers" inverse="true" cascade="all,delete-orphan" >
         <key>
            <column name="questionId" index="answer_questionId_idx" not-null="true"/>
         </key>
         <one-to-many class="entities.Answer"  />
      </bag>

      <property name="created" update="false" >
         <column name="created" not-null="true" index="answer_created_idx"></column>
      </property>
      <property name="lastUpdated">
         <column name="lastUpdated"  not-null="true" index="answer_lastUpdated_idx"></column>
      </property>
   </class>
</hibernate-mapping>


Answer.hbm.xml

Code:
<hibernate-mapping>
   <class name="entities.Answer">
      <id name="id">
         <generator class="native" />
      </id>

      <property name="answer" type="text" not-null="true">
      </property>

      <property name="created" update="false" >
            <column not-null="true" name="created" index="question_created_idx"></column>
      </property>

      <property name="lastUpdated" >
            <column name="lastUpdated" not-null="true" index="question_lastUpdated_idx"></column>
                  </property>

      <many-to-one  name="question" column="questionId" not-null="true" update="false">
      </many-to-one>
   </class>
</hibernate-mapping>


There's 1 Question and 2 answers in my database, this test code:
Code:
Session session = factory.openSession();
Transaction t = session.beginTransaction();
Question q = (Question) session.load(Question.class,1);
session.delete(q);
t.commit();
session.close();


I would expect it to generate SQL like,

Quote:
select .... from Questions where id = 1;
delete from Answers where questionId=1;
delete from Question where id=1;


I.e., just issue one delete to do the cascading delete on Answers,
instead it's loading all the answers and issuing one delete per answer, like:

Code:
    select
        question0_.id as id0_0_,
        question0_.title as title0_0_,
        question0_.question as question0_0_,
        question0_.created as created0_0_,
        question0_.lastUpdated as lastUpda5_0_0_
    from
        Question question0_
    where
        question0_.id=?

    select
        answers0_.questionId as questionId0_1_,
        answers0_.id as id1_,
        answers0_.id as id1_0_,
        answers0_.answer as answer1_0_,
        answers0_.created as created1_0_,
        answers0_.lastUpdated as lastUpda4_1_0_,
        answers0_.questionId as questionId1_0_
    from
        Answer answers0_
    where
        answers0_.questionId=?

    delete   from   Answer  where     id=?
    delete   from   Answer  where     id=?
    delete   from   Question where     id=?


How come, and is there anything I can do about it ?


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

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.