13
I can lazy load one-to-many and many-to-one associations but I can't with the many-to-many associations.
We have a city in which we have merchants which have addresses. Merchants can have multiple addresses and multiple merchants can have the same addresses.
When we load a merchant with a get,
Merchant merchant = (Merchant) hib_session.get(Merchant.class, id);
System.out.println(merchant.getName());
it's ok, addresses aren't load until we iterate over them.
But when we load a list of merchants,
City city = (City) hib_session.get(City.class, city_name);
for(Merchant merchant : city.getMerchants()) {
System.out.println(merchant.getName());
}
even if we don't get the addresses, hibernate automatically loads them.
Here's an example of my problem.
The mapping:
<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"/>