-->
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.  [ 5 posts ] 
Author Message
 Post subject: Confused about mapping
PostPosted: Wed Sep 06, 2006 6:32 am 
Beginner
Beginner

Joined: Fri Jul 07, 2006 12:08 pm
Posts: 37
I have this scenario-A TransportPackage can have many vehicle hire rates.So,my mapping looks like
Code:
  <class name="TransportPackage" table="TransportPackages" lazy ="false" >
    <id name="m_TransportPackageID" column ="TransportPackageID" access ="field">
      <generator class ="identity"></generator>
    </id>

    <property name="TransportPackageName" />
    <many-to-one name="m_Destination" access ="field" class="Destination"  column ="TransportPackageLocationID"/>
   
    <property name="TransportPackageNoOfDays"/>
   

    </bag>
    <bag name ="m_VehicleHireRateCollection" access ="field"  table="TransportPackageVehicleRates" cascade ="all-delete-orphan">
      <key column ="TransportPackageID"></key>
      <one-to-many class ="VehicleHireRate" />
    </bag>

  </class>


and in another hbm..VehicleHireRate
Code:
<class name="VehicleHireRate" table="TransportPackageVehicleRates" lazy ="false">
    <id column ="TransportPackageVehicleRateID" name ="m_VehicleHireRate" access ="field">
      <generator class ="identity"/>
    </id>

    <property column ="TransportPackageVehicleRateBaseCost" name ="m_BaseCost" access ="field"/>

    <many-to-one class ="TransportPackage" column ="TransportPackageID" name ="m_TransportPackage" access ="field"></many-to-one>
  </class>


Now,if I remove many-to-one mapping from vehicleHireRate,I get an error while inserting into VehicleHireRate table,cannot insert null for TransportPackageID.Does it mean,that both sides of relationship must hav references to each other
thks


Top
 Profile  
 
 Post subject: Re: Confused about mapping
PostPosted: Thu Sep 07, 2006 5:01 am 
Beginner
Beginner

Joined: Wed Aug 03, 2005 8:06 am
Posts: 40
Location: Netherlands
How do you ‘insert’ into VehicleHireRate? When you create a VehicleHireRate object, set its properties (among which the TransportPackage property) and flush the session, then you will need the bi-directional relationship. If you add a VehicleHireRate object to the VehicleHireRateCollection of the TransportPackage, either explicitly or by some AddVehicleHireRate method, you can do without the bi-directional relationship (which I usually prefer), but then you must allow the database column TransportPackageVehicleRates.TransportPackageID to be null. See NHibernate doc on One-To-Many Associations.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 09, 2006 2:11 am 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
Have you tried putting inverse='true' in your collection declaration?
Like so:

<bag name ="m_VehicleHireRateCollection" access ="field" table="TransportPackageVehicleRates" cascade ="all-delete-orphan" inverse="true">
<key column ="TransportPackageID"></key>
<one-to-many class ="VehicleHireRate" />
</bag>

Then you can drop the many-to-one association but the key from TransportPackage should be in the TransportPackageVehicleRates table.

MIKE

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 11, 2006 6:35 am 
Beginner
Beginner

Joined: Fri Jul 07, 2006 12:08 pm
Posts: 37
Hi Mike,
I did what you said,I get error cannot insert null..when I add VehicleHireRate and save it..

mnichols wrote:
Have you tried putting inverse='true' in your collection declaration?
Like so:

<bag name ="m_VehicleHireRateCollection" access ="field" table="TransportPackageVehicleRates" cascade ="all-delete-orphan" inverse="true">
<key column ="TransportPackageID"></key>
<one-to-many class ="VehicleHireRate" />
</bag>

Then you can drop the many-to-one association but the key from TransportPackage should be in the TransportPackageVehicleRates table.

MIKE


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 11, 2006 9:17 am 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
Sorry - I was wrong...I just had to get this for a mapping myself.
Per the Hibernate In Action book, when you don't have bidirectional association for a one-to-many, then the foreigh keey column (TransportPackageID) must be null. They admit that is confusing from a relational point of view since typically you'd make it non-nullable, but they argue that since you don't have a bidirectional association in the object, then it is conciveable that the VehicleHireRate would exist on it's own as far as NHibernate is concerned.
So you have a choice - either allow nulls in your foreign key column which may not be a big deal (and remove the inverse='true' flag, or set up the bidirectional association.

I applied this to my mappings as well and am deleting, saving, and updating without bidirectional associations.

Hope this helps.

MIKE

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.