---
title: "hibernate的关联奇怪的地方"
author: "kyo100900"
date: "2008-01-01T21:57:55Z"
tags: []
categories: ["非技术"]
slug: "hibernate"
draft: false
lastmod: "2008-01-01T21:57:55Z"
id: 152198
url: "https://superleo.iteye.com/blog/152198"
---

1\. 在一对多的双向关联里，如果通过关联直接取出对象，比如(这里默认关联lazy="false")

 

```
TestGroup father = (TestGroup) session.load(TestGroup.class,"402881ee16d8fc090116d8fc0b0b0001");List<TestGroup> children = father .getTestGroups();
```

 

以下设置是等价的：

```
		<bag inverse="true" cascade="all" name="testGroups"fetch="subselect" lazy="false"><key column="fk_parent_id" /><one-to-many class="com.leo.domain.TestGroup" /></bag>
```

和

```
		<bag inverse="true" cascade="all" name="testGroups"fetch="select" lazy="false"><key column="fk_parent_id" /><one-to-many class="com.leo.domain.TestGroup" /></bag>
```

 

 

如果是先取出一个集合，比如

```
List<TestGroup> fathers = session.createQuery("from TestGroup g where g.id in ('402881ee16d8fc090116d8fc0b0b0001', '402881ee173520760117352078ae0001')").list();// List<TestGroup> lists = father.getTestGroups();for (TestGroup g : fathers) {System.out.println("father name is: " + g.getName());List<TestGroup> children = g.getTestGroups();for (TestGroup c : children) {System.out.println("children name is : " + c.getName());}}
```

 

 

 

再通过关联

 

以下设置是等价的：

```
		<bag inverse="true" cascade="all" name="testGroups"fetch="subselect" lazy="false"><key column="fk_parent_id" /><one-to-many class="com.leo.domain.TestGroup" /></bag>
```

和

```
		<bag inverse="true" cascade="all" name="testGroups"fetch="join" lazy="false"><key column="fk_parent_id" /><one-to-many class="com.leo.domain.TestGroup" /></bag>
```

我看了控制台输出的SQL语句， 发现结果如我上面所说的那样，不知道对不对？

Hibernate的抓取策略真不明白啊。