昆明java培训
达内昆明广州春城路

18487146383

热门课程

Hibernate:关联级别的检索策略

  • 时间:2016-10-21
  • 发布:昆明Java培训
  • 来源:达内新闻

昆明Java培训班的老师今天给大家讲Hibernate的关联级别的检索策略。

1.指的是用<set>元素来配置的多对一和多对多的关联关系。

2.主要指的是<set>元素的三个属性:lazy、fetch、batch-size。

3. lazy属性(默认为true,即采用延迟检索策略)

(1)决定集合被初始化的时机。

(2)取值为true时

Hibernate会在以下情况下初始化集合代理类实例

访问集合属性、iterator()、size()、isEmpty()、contains()等方法时

昆明Java培训班的老师通过Hibernate.initialize()静态方法显示的初始化

(3)取值为false时

(4)取值为extra时(增强的延迟检索策略),会尽可能的延迟初始化集合的时机。如:

@Test

public void testSetLazy() {

Category category = (Category) session.get(Category.class, 5);

System.out.println(category.getItems().size());

}

Hibernate:

select

category0_.category_id as category1_1_0_,

category0_.category_name as category2_1_0_

from

hibernate.category category0_

where

category0_.category_id=?

Hibernate:

select

count(item_id)

from

hibernate.categories_items

where

category_id =?

2

调用集合的size()方法时,是通过count()来查询的。

当调用集合的iterator()方法时,会初始化集合。

4. fetch属性(默认为"select")

(1)取值为"select"或"subselect"时,决定初始化集合查询语句的形式。

(2)取值为"join",则决定初始化集合的时机。会忽略"lazy"属性。

(3)测试

<1>取值为"select"

@Test

public void testSetLazy() {

List<Category> categories = session.createQuery("from Category").list();

for(Category category : categories) {

System.out.println(category.getItems().size());

}

}

Hibernate:

select

customer0_.customer_id as customer1_2_,

customer0_.customer_name as customer2_2_

from

hibernate.customer customer0_

Hibernate:

select

orders0_.customer_id as customer3_2_1_,

orders0_.order_id as order1_4_1_,

orders0_.order_id as order1_4_0_,

orders0_.order_name as order2_4_0_,

orders0_.customer_id as customer3_4_0_

from

hibernate.order orders0_

where

orders0_.customer_id=?

2

Hibernate:

select

orders0_.customer_id as customer3_2_1_,

orders0_.order_id as order1_4_1_,

orders0_.order_id as order1_4_0_,

orders0_.order_name as order2_4_0_,

orders0_.customer_id as customer3_4_0_

from

hibernate.order orders0_

where

orders0_.customer_id=?

2

Hibernate:

select

orders0_.customer_id as customer3_2_1_,

orders0_.order_id as order1_4_1_,

orders0_.order_id as order1_4_0_,

orders0_.order_name as order2_4_0_,

orders0_.customer_id as customer3_4_0_

from

hibernate.order orders0_

where

orders0_.customer_id=?

2

Hibernate:

select

orders0_.customer_id as customer3_2_1_,

orders0_.order_id as order1_4_1_,

orders0_.order_id as order1_4_0_,

orders0_.order_name as order2_4_0_,

orders0_.customer_id as customer3_4_0_

from

hibernate.order orders0_

where

orders0_.customer_id=?

2

<2>取值为"subselect",会忽略batch-size属性。

Hibernate:

select

category0_.category_id as category1_1_,

category0_.category_name as category2_1_

from

hibernate.category category0_

Hibernate:

select

items0_.category_id as category1_1_1_,

items0_.item_id as item2_0_1_,

item1_.item_id as item1_3_0_,

item1_.item_name as item2_3_0_

from

hibernate.categories_items items0_

inner join

hibernate.item item1_

on items0_.item_id=item1_.item_id

where

items0_.category_id in (

?, ?

)

2

2

Hibernate:

select

customer0_.customer_id as customer1_2_,

customer0_.customer_name as customer2_2_

from

hibernate.customer customer0_

Hibernate:

select

orders0_.customer_id as customer3_2_1_,

orders0_.order_id as order1_4_1_,

orders0_.order_id as order1_4_0_,

orders0_.order_name as order2_4_0_,

orders0_.customer_id as customer3_4_0_

from

hibernate.order orders0_

where

orders0_.customer_id in (

select

customer0_.customer_id

from

hibernate.customer customer0_

)

2

2

2

2

通过子查询的方式,通过in的方式。

<3>取值为"join"

会采用迫切左外链接(使用左外链接进行查询,同时初始化集合属性)策略来初始化所有关联的对象

lazy属性将会被忽略

Query的list()方法会忽略这个取值,不会忽略lazy属性。

HQL会忽略取值为"join"的取值

@Test

public void testFetch() {

session.get(Customer.class, 5);

}

Hibernate:

select

customer0_.customer_id as customer1_2_1_,

customer0_.customer_name as customer2_2_1_,

orders1_.customer_id as customer3_2_3_,

orders1_.order_id as order1_4_3_,

orders1_.order_id as order1_4_0_,

orders1_.order_name as order2_4_0_,

orders1_.customer_id as customer3_4_0_

from

hibernate.customer customer0_

left outer join

hibernate.order orders1_

on customer0_.customer_id=orders1_.customer_id

where

customer0_.customer_id=?

5.batch-size属性,设定批量检索集合的数量。

(1)默认情况下

@Test

public void testSetLazy() {

List<Category> categories = session.createQuery("from Category").list();

for(Category category : categories) {

System.out.println(category.getItems().size());

}

}

Hibernate:

select

category0_.category_id as category1_1_,

category0_.category_name as category2_1_

from

hibernate.category category0_

Hibernate:

select

items0_.category_id as category1_1_1_,

items0_.item_id as item2_0_1_,

item1_.item_id as item1_3_0_,

item1_.item_name as item2_3_0_

from

hibernate.categories_items items0_

inner join

hibernate.item item1_

on items0_.item_id=item1_.item_id

where

items0_.category_id=?

2

Hibernate:

select

items0_.category_id as category1_1_1_,

items0_.item_id as item2_0_1_,

item1_.item_id as item1_3_0_,

item1_.item_name as item2_3_0_

from

hibernate.categories_items items0_

inner join

hibernate.item item1_

on items0_.item_id=item1_.item_id

where

items0_.category_id=?

2

(2)设置batch-size="2"

@Test

public void testSetLazy() {

List<Category> categories = session.createQuery("from Category").list();

for(Category category : categories) {

System.out.println(category.getItems().size());

}

}

Hibernate:

select

category0_.category_id as category1_1_,

category0_.category_name as category2_1_

from

hibernate.category category0_

Hibernate:

select

items0_.category_id as category1_1_1_,

items0_.item_id as item2_0_1_,

item1_.item_id as item1_3_0_,

item1_.item_name as item2_3_0_

from

hibernate.categories_items items0_

inner join

hibernate.item item1_

on items0_.item_id=item1_.item_id

where

items0_.category_id in (

?, ?

)

2

2

了解详情请登陆昆明达内Java培训官网(km.Java.tedu.cn)!

上一篇:Hibernate:检索策略三个知识点
下一篇:Java培训:many-to-one>元素的lazy和fetch属性

昆明Java培训班《Java分布式数据库的未来》

昆明java培训班:为什么java工程师薪资这么高?

昆明java培训班:你所不知道的java秘密

昆明java培训班;如何认识Java Web技术

选择城市和中心
贵州省

广西省

海南省

扫一扫

了解更多干货