我想知道为什么hibernate在子表上为每个实体生成1个删除 而不是在外键上使用一个删除
这是hibernate.cfg.xml(不,我不是下一个SO:-t
<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
<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
<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>
我的数据库中有1个问题和2个答案,此测试代码:
Session session = factory.openSession();
Transaction t = session.beginTransaction();
Question q = (Question) session.load(Question.class,1);
session.delete(q);
t.commit();
session.close();
我希望它能像SQL一样生成SQL,
select .... from Questions where id = 1;
delete from Answers where questionId=1;
delete from Question where id=1;
即,只需发出一个删除来对Answers进行级联删除, 相反,它会加载所有答案并为每个答案发出一个删除,例如:
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=?
为什么我能做些什么?
编辑,响应Nate Zaugg,我可以通过在一对多键映射上设置on-delete =“cascade”来让db进行级联删除,我更想知道为什么hibernate会做什么呢会不会在Answers表上删除,而且我的映射有问题。
答案 0 :(得分:1)
您是否可以不将DMBS配置为对关系进行级联删除?这很容易做到。
修改:试试这个<one-to-many class="entities.Answer" lazy="false" cascade="all" />