Hibernate抓取策略(fetch)
一、单端关联上的fetch[取值:select(默认)/join]
测试用例:
TBook b = (TBook)session.get(TBook.class,1);
System.out.println("图书名称:"+b.getBName());
System.out.println("图书类别:"+b.getCategory().getCategory());
1)保持默认,相当于fetch="select"
如:<many-to-one name="category" class="cn.bdqn.pojo.Category" cascade="all" fetch="select">
数据抓取过程中,select抓取会另外发送一条select语句来抓取与当前对象相关联的实体或集合。
执行结果:2条语句
Hibernate: select tbook0_.id as id1_1_0_, tbook0_.category_Id as category2_1_0_, tbook0_.author as author3_1_0_, tbook0_.b_name as b_name4_1_0_, tbook0_.b_price as b_price5_1_0_, tbook0_.pubDate as pubDate6_1_0_ from admindb.t_book tbook0_ where tbook0_.id=?
图书名称:红楼梦
Hibernate: select category0_.id as id1_0_0_, category0_.category as category2_0_0_ from admindb.category category0_ where category0_.id=?
图书类别:文学
2)设置fetch="join"
如:<many-to-one name="category" class="cn.bdqn.pojo.Category" cascade="all"fetch="join">
数据抓取过程中,join抓取会通过select语句使用外连接来加载其关联实体或集合。
执行结果:1条语句