Note that there are some explanatory texts on larger screens.

plurals
  1. POhibernate cascading delete, why not one delete on the foreign key?
    primarykey
    data
    text
    <p>I'm wondering why hibernate generates 1 delete per entity on a child table instead of using one delete on the foreign key</p> <p>Here's the hibernate.cfg.xml (No i's not the next SO :-t</p> <pre><code>&lt;hibernate-configuration&gt; &lt;session-factory&gt; &lt;property name="hibernate.connection.url"&gt;jdbc:hsqldb:file:testdb;shutdown=true&lt;/property&gt; &lt;property name="hibernate.connection.driver_class"&gt;org.hsqldb.jdbcDriver&lt;/property&gt; &lt;property name="hibernate.connection.username"&gt;sa&lt;/property&gt; &lt;property name="hibernate.connection.password"&gt;&lt;/property&gt; &lt;property name="hibernate.connection.pool_size"&gt;0&lt;/property&gt; &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.HSQLDialect&lt;/property&gt; &lt;property name="hibernate.show_sql"&gt;true&lt;/property&gt; &lt;property name="hibernate.format_sql"&gt;true&lt;/property&gt; &lt;property name="hbm2ddl.auto"&gt;auto&lt;/property&gt; &lt;mapping file="entities/Question.hbm.xml"/&gt; &lt;mapping file="entities/Answer.hbm.xml"/&gt; &lt;/session-factory&gt; </code></pre> <p>Question.hbm.xml</p> <pre><code>&lt;hibernate-mapping&gt; &lt;class name="entities.Question"&gt; &lt;id name="id"&gt; &lt;generator class="native" /&gt; &lt;/id&gt; &lt;property name="title" not-null="true"&gt; &lt;/property&gt; &lt;property name="question" type="text" not-null="true"&gt; &lt;/property&gt; &lt;bag name="answers" inverse="true" cascade="all,delete-orphan" &gt; &lt;key&gt; &lt;column name="questionId" index="answer_questionId_idx" not-null="true"/&gt; &lt;/key&gt; &lt;one-to-many class="entities.Answer" /&gt; &lt;/bag&gt; &lt;property name="created" update="false" &gt; &lt;column name="created" not-null="true" index="answer_created_idx"&gt;&lt;/column&gt; &lt;/property&gt; &lt;property name="lastUpdated"&gt; &lt;column name="lastUpdated" not-null="true" index="answer_lastUpdated_idx"&gt;&lt;/column&gt; &lt;/property&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>Answer.hbm.xml</p> <pre><code>&lt;hibernate-mapping&gt; &lt;class name="entities.Answer"&gt; &lt;id name="id"&gt; &lt;generator class="native" /&gt; &lt;/id&gt; &lt;property name="answer" type="text" not-null="true"&gt; &lt;/property&gt; &lt;property name="created" update="false" &gt; &lt;column not-null="true" name="created" index="question_created_idx"&gt;&lt;/column&gt; &lt;/property&gt; &lt;property name="lastUpdated" &gt; &lt;column name="lastUpdated" not-null="true" index="question_lastUpdated_idx"&gt;&lt;/column&gt; &lt;/property&gt; &lt;many-to-one name="question" column="questionId" not-null="true" update="false"&gt; &lt;/many-to-one&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>There's 1 Question and 2 answers in my database, this test code:</p> <pre><code>Session session = factory.openSession(); Transaction t = session.beginTransaction(); Question q = (Question) session.load(Question.class,1); session.delete(q); t.commit(); session.close(); </code></pre> <p>I would expect it to generate SQL like,</p> <pre><code>select .... from Questions where id = 1; delete from Answers where questionId=1; delete from Question where id=1; </code></pre> <p>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:</p> <pre><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=? </code></pre> <p>How come, and is there anything I can do about it ?</p> <p>Edit, in response to Nate Zaugg, I can get the db to do the cascading delete by setting on-delete="cascade" on the one-to-many key mapping, i'm more wondering why hibernate does what it does and not does one delete on the Answers table, and wheter threre's something wrong with my mappings.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload