I am having trouble with many-to-one relationship. I am unable to retrieve the foreign key and get a LazyInitializationException error. I have set the many-to-one property to lazy="false" and I have also set the class to default-lazy="false". When I am looking at the SQL Query produced by Hibernate, I see that it is not fetching the foreign keys. It used to work fine in Hibernate 2, but with Hibernate 3 the foreign keys are not working. I have one one-to-many relationship which I set lazy="false" and that particular one works fine. It is strange I would think the one-to-many would not be fetched but the many-to-one should be fetched.
Products.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.at.hib.persistence" [b]default-lazy="false"[/b]>
<class name="Products" table="products">
<id
column="Products_ID"
name="Id"
type="integer"
>
<generator class="native" />
</id>
<property
column="Starting_Price"
length="7"
name="StartingPrice"
not-null="false"
type="big_decimal"
/>
<property
column="Internal_Flights"
length="11"
name="InternalFlights"
not-null="false"
type="integer"
/>
<property
column="Internal_Flights_Included"
length="11"
name="InternalFlightsIncluded"
not-null="false"
type="integer"
/>
<property
column="Map"
length="200"
name="Map"
not-null="false"
type="string"
/>
<property
column="Ref_ID"
name="RefId"
not-null="false"
type="string"
/>
<property
column="Title"
length="200"
name="Title"
not-null="false"
type="string"
/>
<property
column="Short_Description"
name="ShortDescription"
not-null="false"
type="string"
/>
<many-to-one
class="Suppliers"
name="Suppliers"
not-null="true"
[b]lazy="false"[/b]
>
<column name="suppliers_id" />
</many-to-one>
<many-to-one
class="Types"
name="Types"
not-null="true"
[b]lazy="false"[/b]
>
<column name="Types_ID" />
</many-to-one>
<many-to-one
class="Quality"
name="Quality"
not-null="true"
[b]lazy="false"[/b]
>
<column name="Quality_ID" />
</many-to-one>
<many-to-one
class="Cities"
name="EndCity"
not-null="true"
>
<column name="end_city" />
</many-to-one>
<many-to-one
class="Cities"
name="StartCity"
not-null="true"
>
<column name="start_city" />
</many-to-one>
<set
inverse="true"
lazy="true"
name="ProductsRegionsSet"
>
<key column="Products_ID" />
<one-to-many class="ProductsRegions" />
</set>
<set
inverse="true"
lazy="true"
name="ProductsCitiesSet"
>
<key column="Products_ID" />
<one-to-many class="ProductsCities" />
</set>
<set
inverse="true"
lazy="true"
name="ImagesSet"
>
<key column="Products_ID" />
<one-to-many class="Images" />
</set>
<set
inverse="true"
lazy="true"
name="TourSpecialsSet"
>
<key column="Products_ID" />
<one-to-many class="TourSpecials" />
</set>
<set
inverse="true"
lazy="true"
name="TourBookingSet"
>
<key column="ItineraryID" />
<one-to-many class="TourBooking" />
</set>
<set
inverse="true"
lazy="true"
name="ProductsCountriesSet"
>
<key column="Products_ID" />
<one-to-many class="ProductsCountries" />
</set>
<set
inverse="true"
lazy="true"
name="QuoteToursSet"
>
<key column="ItineraryID" />
<one-to-many class="QuoteTours" />
</set>
<set
inverse="true"
lazy="true"
name="ProductsBrochureSet"
>
<key column="Products_ID" />
<one-to-many class="ProductsBrochure" />
</set>
<set
inverse="true"
lazy="true"
name="ProductsDatesRatesSet"
>
<key column="Products_ID" />
<one-to-many class="ProductsDatesRates" />
</set>
<set
inverse="true"
lazy="false"
name="ProductsCodesSet"
>
<key column="Products_ID" />
<one-to-many class="ProductsCodes" />
</set>
<set
inverse="true"
lazy="true"
name="ProductsSourceCodeSet"
>
<key column="Products_ID" />
<one-to-many class="ProductsSourceCode" />
</set>
</class>
</hibernate-mapping>