我可以懶加載一對多和多對一的關聯,但我不能與多對多的關聯。如何在休眠中延遲加載多對多的集合?
我們有一個城市,我們有商人有很多地址。 商家可以有多個地址,多個商家可以擁有相同的地址。
當我們加載一個商人用GET,
Merchant merchant = (Merchant) hib_session.get(Merchant.class, id);
System.out.println(merchant.getName());
它的確定,地址不會加載,直到我們遍歷他們。
但是,當我們加載的商家列表,
City city = (City) hib_session.get(City.class, city_name);
for(Merchant merchant : city.getMerchants()) {
System.out.println(merchant.getName());
}
即使我們沒有得到的地址,自動休眠加載它們。
Here's an example of my problem.
映射:
<class name="Merchant" table="Merchants" lazy="true">
<id name="id"
type="long"
column="id">
<generator class="native"></generator>
</id>
<set name="addresses" table="AdressesMerchant" lazy="true">
<key column="merchant_id"></key>
<many-to-many class="Adresses" column="address_id"/>
</set>
</class>
<class name="Address" table="Adresses" lazy="true">
<id name="id"
type="long"
column="id">
<generator class="native"></generator>
</id>
<set name="merchants" table="AdressesMerchant" lazy="true">
<key column="adress_id"/>
<many-to-many column="merchant_id" class="Merchant"/>
</set>
</class>
任何想法?
聽起來很奇怪。你能證實這種行爲嗎?你的藏品如何映射? – Bozho 2011-01-26 14:50:43
@Bozho我可以通過記錄查詢來確認行爲,並且我看到Hibernate加載了地址。我在問題中添加了映射。 – codea 2011-01-26 15:07:44
這不是主題,但不應該成爲標記爲逆的多對多關係之一? – Ralph 2011-01-26 16:08:39