检索策略


可以延迟加载的配置组合:

⑴关联实体的载入策略(one-to-one或many-to-one

lazy

决定关联实体什么时候载入 (proxy|false)

fetch

join:使用连接查询检索对象;select(默认):抓取在前面查询到的所有实体对象的关联对象

 

类级别:在调用load函数时延迟有效,get函数无效,Query.list()都无效.

                   默认 延迟加载.

       

延迟加载:

Customer.hbm.xml:

[html]  view plain  copy
  1. <class name="basicCar.bean.Customer" table="customer" lazy="true">  
  2.       <!--   一对多,客户对账号-->  
  3.     <set name="Accounts" inverse="false" cascade="delete" fetch="select" lazy="extra">  
  4.     <key column="customerId"/>  
  5.     <one-to-many class="basicCar.bean.Account"/>  
  6.     </set>  
  7. </class>  


Account.hbm.xml:

[html]  view plain  copy
  1. <class name="basicCar.bean.Account" table="account" lazy="false">  
  2. <!-- 
  3.     多对一,账号对用户 
  4.     -->  
  5.     <many-to-one name="customer"   
  6.                 column="customerId"   
  7.                 class="basicCar.bean.Customer"   
  8.                 cascade="save-update"  
  9.                 not-null="false"  
  10.                                 fetch="select"   
  11.                                 lazy="proxy">  
  12.    </many-to-one>  
  13. </class>  


立即加载:

Customer.hbm.xml:

[html]  view plain  copy
  1. <class name="basicCar.bean.Customer" table="customer" lazy="false">  
  2.       <!--   一对多,客户对账号-->  
  3.     <set name="Accounts" inverse="false" cascade="delete" fetch="select" lazy="extra">  
  4.     <key column="customerId"/>  
  5.     <one-to-many class="basicCar.bean.Account"/>  
  6.     </set>  
  7. </class>  


Account.hbm.xml:

[html]  view plain  copy
  1. <class name="basicCar.bean.Account" table="account" lazy="false">  
  2. <!-- 
  3.     多对一,账号对用户 
  4.     -->  
  5.     <many-to-one name="customer"   
  6.                 column="customerId"   
  7.                 class="basicCar.bean.Customer"   
  8.                 cascade="save-update"  
  9.                 not-null="false"  
  10.                                 fetch="select"   
  11.                                 lazy="false">  
  12.    </many-to-one>  
  13. </class>  

batch载入策略:

设置批量处理,可以把缓存中的代理类进行批量处理,这样可以减少数据库的访问次数.

例如在Customer.hbm.xml:

    <set name="Accounts" inverse="false" cascade="delete" fetch="select" lazy="extra"batch-size="3" > <!-- 表示最多批量初始化3个accounts集合代理类 -->

        <key column="customerId"/>

         <one-to-many class="basicCar.bean.Account"/>

    </set>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值