many-to-many性能

hibernatemany-to-many关系中,user<1...*---1...*>role,当删除inverse="true"(user)一端的一个实例时,需要循环的从inverse="false"(role)一端查找与user的关系并删除之,最后删除inverse="true"的一端,hibernate会产生以下的sql语句:


1)
一个select from user.
2)
一个
select from user_role.
3)2*n
(userroles个数
)select from user_role.
4)
一个
select from user.
5)n
delete from user_role.
6)
一个delete form user.

 

下面是删除一个user所产生的sql语句.user具有3role.

select authuser0_.ID as ID0_, authuser0_.password as password0_, authuser0_.username   as username0_ from auth_user authuser0_ where authuser0_.ID=?

select authroles0_.roleID as roleID__, authroles0_.userID as userID__ from auth_user_role authroles0_ where authroles0_.userID=?

  select authrole0_.ID as ID0_, authrole0_.name as name0_, authrole0_.description as descript3_0_ from auth_role authrole0_ where authrole0_.ID=?

 

select authrole0_.ID as ID0_, authrole0_.name as name0_, authrole0_.description as descript3_0_ from auth_role authrole0_ where authrole0_.ID=?

 

select authrole0_.ID as ID0_, authrole0_.name as name0_, authrole0_.description as descript3_0_ from auth_role authrole0_ where authrole0_.ID=?

 

select authusers0_.userID as userID__, authusers0_.roleID as roleID__ from auth_user_role  authusers0_ where authusers0_.roleID=?

 

select authusers0_.userID as userID__, authusers0_.roleID as roleID__ from auth_user_role authusers0_ where authusers0_.roleID=?

 

select authusers0_.userID as userID__, authusers0_.roleID as roleID__ from auth_user_role authusers0_ where authusers0_.roleID=?

 

select authuser0_.ID as ID0_, authuser0_.password as password0_, authuser0_.username as username0_ from auth_user authuser0_ where authuser0_.ID=?

 

select authuser0_.ID as ID0_, authuser0_.password as password0_, authuser0_.username as username0_ from auth_user authuser0_ where authuser0_.ID=?

 

delete from auth_user_role where roleID=?

 

delete from auth_user_role where roleID=? and userID=?

 

delete from auth_user_role where roleID=?

 

delete from auth_user where ID=?

此时set各属性如下:

user:

<set

name="authRoles"

table="auth_user_role"

lazy="true"

inverse="true"

cascade="save-update"

sort="unsorted"

>

<key

column="userID"

/>

<many-to-many

class="cn.bluewind.security.model.AuthRole"

column="roleID"

outer-join="auto"

/>

</set>

 

 

role:

 

<set

name="authUsers"

table="auth_user_role"

lazy="true"

inverse="false"

cascade="save-update"

sort="unsorted"

>

<key

column="roleID"

/>

<many-to-many

class="cn.bluewind.security.model.AuthUser"

column="userID"

outer-join="auto"

/>

</set>


此时数据库中中间表user_role中的外间约束是delete->restrict.

但是如果手动的在ddl中将外键约束改为delete->cascade,在删除user,就不需要由程序先删除user_role之间的关系,而是由数据库代劳了.此时产生的sql如下:

 

select authuser0_.ID as ID0_, authuser0_.password as password0_, authuser0_.username as username0_ from auth_user authuser0_ where authuser0_.ID=?

 

delete from auth_user where ID=?

两个sql而已.比起上面来少了很多.这样对性能的提升是否有好处,这种方式是否正确.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值