-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: many-to-one relationship unable to retrieve foreign key HB3
PostPosted: Mon May 09, 2005 5:09 pm 
Regular
Regular

Joined: Wed Sep 29, 2004 11:34 am
Posts: 62
Location: Houston, TX
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>


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 09, 2005 5:26 pm 
Newbie

Joined: Mon May 09, 2005 2:29 pm
Posts: 2
Location: Irvine, CA
I experienced something similar a week or two ago. My initial work-around was to set lazy="false" on the destination class of each foreign key. In your case, that would be classes like Types, Quality, Cities, and so forth. That got me up an running with no lazy references.

After that, I spent some time tweaking Spring until I found a way to keep the Hibernate session alive for the duration of the web request. That was necessary to allow the lazy references to work as lazy references. The techniques for doing that vary from framework to framework. If you provide a little more background about your project I will try to point you in the right direction.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 09, 2005 11:45 pm 
Regular
Regular

Joined: Wed Sep 29, 2004 11:34 am
Posts: 62
Location: Houston, TX
Thank you John, that hit the spot. I had to put default-lazy="false" on all my hbm files to ensure that all many-to-one relationships can be visible. I manually set the lazy=false or lazy=true on the one-to-many relationships.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.