欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hibernate学习笔记第22讲-抓取策略

程序员文章站 2022-05-15 15:55:56
...

 

抓取策略:抓取关联对象

 

1单端代理的批量抓取 select   join   默认是select 

保持默认,同fetch="select",如:

<many-to-one name="classes" column="classesid" fetch="select"/>

fetch="select",另外发送一条select语句抓取当前对象关联实体或集合

<hibernate-mapping>

     <class name="com.bjsxt.hibernate.Student" table="t_student">

              <!--

              <cache usage="read-only"/>

               -->

              <id name="id">

                       <generator class="native"/>

              </id>

              <property name="name"/>

              <many-to-one name="classes" column="classesid" fetch="select"/>

     </class>

</hibernate-mapping>

 

设置fetch="join",如:

<many-to-one name="classes" column="classesid" fetch="join"/>

 

fetch="join",hibernate会通过select语句使用外连接来加载其关联实体或集合

 

只发一条sql语句,此时lazy会失效

2,集合上的抓取策略:集合代理的批量抓取

保持默认,同fetch="select",如:

<set name="students" inverse="true" cascade="all" fetch="select">

 

fetch="select",另外发送一条select语句抓取当前对象关联实体或集合

<hibernate-mapping package="com.bjsxt.hibernate">

     <class name="Classes" table="t_classes">

              <id name="id">

                       <generator class="native"/>

              </id>

              <property name="name"/>

              <set name="students" inverse="true" cascade="all" fetch="select">

                       <key column="classesid"/>

                       <one-to-many class="Student"/>

              </set>

     </class>

</hibernate-mapping>

设置fetch="join",如:

<set name="students" inverse="true" cascade="all" fetch="join">

fetch="join",hibernate会通过select语句使用外连接来加载其关联实体或集合

此时lazy会失效

 

设置fetch="subselect", 影响hql查询如:

<set name="students" inverse="true" cascade="all" fetch="subselect">

fetch="subselect",另外发送一条select语句抓取在前面查询到的所有实体对象的关联集合

 

3hibernate抓取策略,,batch-szie<class>上的应用

 

batch-size属性,可以批量加载实体类,参见:Classes.hbm.xml

<class name="Classes" table="t_classes" batch-size="3">