Beautiful SQL

Given a complex mapping like this

<class name="Animal">
<id name="id">
<generator class="native"/>
</id>
<property name="description"/>
<property name="bodyWeight" column="body_weight"/>
<many-to-one name="mother" column="mother_id"/>
<many-to-one name="father" column="father_id"/>
<many-to-one name="zoo" column="zoo_id"/>
<property name="serialNumber"/>
<set name="offspring" order-by="father_id">
<key column="mother_id"/>
<one-to-many class="Animal"/>
</set>
<joined-subclass name="Reptile">
<key column="animal"/>
<property name="bodyTemperature"/>
<joined-subclass name="Lizard">
<key column="reptile"/>
</joined-subclass>
</joined-subclass>
<joined-subclass name="Mammal">
<key column="animal"/>
<property name="pregnant"/>
<property name="birthdate" type="date"/>
<joined-subclass name="DomesticAnimal">
<key column="mammal"/>
<many-to-one name="owner"/>
<joined-subclass name="Cat">
<key column="mammal"/>
</joined-subclass>
<joined-subclass name="Dog">
<key column="mammal"/>
</joined-subclass>
</joined-subclass>
<joined-subclass name="Human">
<key column="mammal"/>
<component name="name">
<property name="first" column="name_first"/>
<property name="initial" column="name_initial"/>
<property name="last" column="name_last"/>
</component>
<property name="nickName"/>
<property name="height"/>

<property name="intValue"/>
<property name="floatValue"/>
<property name="bigDecimalValue"/>
<property name="bigIntegerValue"/>

<bag name="friends">
<key column="human1"/>
<many-to-many column="human2" class="Human"/>
</bag>
<map name="family">
<key column="human1"/>
<map-key column="relationship" type="string"/>
<many-to-many column="human2" class="Human"/>
</map>
<bag name="pets" inverse="true">
<key column="owner"/>
<one-to-many class="DomesticAnimal"/>
</bag>
<set name="nickNames" lazy="false" table="human_nick_names" sort="natural">
<key column="human"/>
<element column="nick_name" type="string" not-null="true"/>
</set>
<map name="addresses" table="addresses">
<key column="human"/>
<map-key type="string" column="type"/>
<composite-element class="Address">
<property name="street"/>
<property name="city"/>
<property name="postalCode"/>
<property name="country"/>
<many-to-one name="stateProvince" column="state_prov_id" class="StateProvince"/>
</composite-element>
</map>
</joined-subclass>
</joined-subclass>
</class>

Which should be the result of s.CreateQuery("delete Animal").ExecuteUpdate() ?

Oh… beautiful SQL

create table #Animal (id BIGINT not null) 

insert into #Animal SELECT animal0_.id as id FROM Animal animal0_

DELETE FROM Human WHERE (mammal) IN (select id from #Animal)
DELETE FROM Dog WHERE (mammal) IN (select id from #Animal)
DELETE FROM Cat WHERE (mammal) IN (select id from #Animal)
DELETE FROM DomesticAnimal WHERE (mammal) IN (select id from #Animal)
DELETE FROM Mammal WHERE (animal) IN (select id from #Animal)
DELETE FROM Lizard WHERE (reptile) IN (select id from #Animal)
DELETE FROM Reptile WHERE (animal) IN (select id from #Animal)
DELETE FROM Animal WHERE (id) IN (select id from #Animal)
drop table #Animal

Do you see the temp table ? Do you see the order of queries ?

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值