Hello,
I am a bit stuck here. I have looked through the documentation but failed to find the answer to the following problem:
I have a relationship between a parent object and two child objects. I want the child objects deleted from persistence when I delete the parent. This works fine except that Hibernate loads the child objects before it deletes them. This adds two additional select statements to the process and creates a large overhead. What have I overlooked? Is there a way to avoid these additional selects?
Here's the mapping, code, and SQL output:
MAPPING
<hibernate-mapping default-cascade="none" auto-import="true">
<class name="com.mycompany.hibercool.example.dto.PersonHB" table="PER_PERSONNE" dynamic-update="false" dynamic-insert="false" mutable="true" polymorphism="implicit" batch-size="1" select-before-update="false" optimistic-lock="version">
<id name="id" column="PK_PER_ID" type="java.lang.Long" unsaved-value="null">
<generator class="assigned" />
</id>
<property name="cellularNumber" type="java.lang.String" update="true" insert="true" column="PER_CELLULAR_NUMBER" length="12" not-null="false" unique="false" />
<property name="email" type="java.lang.String" update="true" insert="true" column="PER_EMAIL" length="30" not-null="false" unique="false" />
<set name="etatsCivils" lazy="true" inverse="true" cascade="delete" sort="unsorted" batch-size="1" outer-join="auto">
<key column="FK_PER_ID" />
<one-to-many class="com.mycompany.hibercool.example.dto.EtatCivilHB" />
</set>
<set name="habilitationsSages" lazy="true" inverse="true" cascade="delete" sort="unsorted" batch-size="1" outer-join="auto">
<key column="FK_PER_ID" />
<one-to-many class="com.mycompany.hibercool.example.dto.HabilitationSagesHB" />
</set>
<property name="phoneNumber" type="java.lang.String" update="true" insert="true" column="PER_PHONE_NUMBER" length="12" not-null="false" unique="false" />
</class>
</hibernate-mapping>
JAVA
int nb = session.delete("from com.mycompany.hibercool.example.dto.PersonHB person where person.id = " + id);
SQL OUTPUT
17:49:50,174 INFO [STDOUT] Hibernate: select personhb0_.PK_PER_ID as PK_PER_ID, personhb0_.PER_CELLULAR_NUMBER as PER_CELL2_, personhb0_.PER_EMAIL as PER_EMAIL, personhb0_.PER_PHONE_NUMBER as PER_PHON4_ from PER_PERSONNE personhb0_ where (personhb0_.PK_PER_ID=100000048564 )
17:49:50,420 INFO [STDOUT] Hibernate: select etatscivil0_.PK_ETC_ID as PK_ETC_ID__, etatscivil0_.FK_PER_ID as FK_PER_ID__, etatscivil0_.PK_ETC_ID as PK_ETC_ID0_, etatscivil0_.ETC_YEAR as ETC_YEAR0_, etatscivil0_.ETC_BIRTHNAME as ETC_BIRT3_0_, etatscivil0_.ETC_FIRSTNAME as ETC_FIRS4_0_, etatscivil0_.ETC_LASTNAME as ETC_LAST5_0_, etatscivil0_.ETC_TITLE as ETC_TITLE0_, etatscivil0_.FK_PER_ID as FK_PER_ID0_ from ETC_ETAT_CIVIL etatscivil0_ where etatscivil0_.FK_PER_ID=? 17:49:50,433 INFO [STDOUT] Hibernate: select habilitati0_.PK_HAS_ID as PK_HAS_ID__, habilitati0_.FK_PER_ID as FK_PER_ID__, habilitati0_.PK_HAS_ID as PK_HAS_ID0_, habilitati0_.HAS_CODE_DSF as HAS_CODE2_0_, habilitati0_.HAS_YEAR as HAS_YEAR0_, habilitati0_.FK_PER_ID as FK_PER_ID0_ from HAS_HABILITATION_SAGES habilitati0_ where habilitati0_.FK_PER_ID=?
17:49:50,438 INFO [STDOUT] Hibernate: delete from ETC_ETAT_CIVIL where PK_ETC_ID=?
17:49:50,440 INFO [STDOUT] Hibernate: delete from HAS_HABILITATION_SAGES where PK_HAS_ID=?
17:49:50,455 INFO [STDOUT] Hibernate: delete from PER_PERSONNE where PK_PER_ID=?
|