检索策略

来源:互联网 发布:科技成果登记软件 编辑:程序博客网 时间:2024/06/10 01:27

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

⑴关联实体的载入策略(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>

0 0
原创粉丝点击