Hibernate面试题部分汇总集合(15)

发布时间:2021-06-06

此文档主要简绍J2EE中的hibernate面试突击问题部分集合

<set name=”addresses” table=”address” lazy=”true” inverse=”true”> <key column=”user_id”/>

<one-to-many class=”com.neusoft.entity.Arrderss”/>

</set>

</class>

</hibernate-mapping>

通过将<set>元素的lazy属性设置为true来开启集合类型的延迟加载特性。我们看下面的代码:

User user=(User)session.load(User.class,”1”);

Collection addset=user.getAddresses(); (1)

Iterator it=addset.iterator(); (2)

while(it.hasNext()){

Address address=(Address)it.next();

System.out.println(address.getAddress());

}

当程序执行到(1)处时,这时并不会发起对关联数据的查询来加载关联数据,只有运行到(2)处时,真正的数据读取操作才会开始,这时Hibernate会根据缓存中符合条件的数据索引,来查找符合条件的实体对象。

这里我们引入了一个全新的概念——数据索引,下面我们首先将接一下什么是数据索引。在 Hibernate中对集合类型进行缓存时,是分两部分进行缓存的,首先缓存集合中所有实体的id列表,然后缓存实体对象,这些实体对象的id列表,就是所谓的数据索引。当查找数据索引时,如果没有找到对应的数据索引,这时

精彩图片

热门精选

大家正在看