I have been struggling with the cascade mapping of two properties that
basically refer to the same object. There are several situations in
which we use this. For example, I have flight instance which maps to
one or more booking instances (Set Bookings) but ONE of these bookings
is considered the main booking (Many-To-One MainBooking). Similar
mapping problem happens with invoices (all booking invoices, current
booking invoice mapping). Part of the flight mapping shown below.
<set name="Bookings"
where="Deleted=0"
inverse="true"
batch-size="4"
cascade="all-delete-orphan">
<cache usage="read-write" region="Flight"/>
<key column="FlightID"/>
<one-to-many class="Booking"/>
</set>
<many-to-one name="MainBooking"
class="Booking"
column="MainBookingID"
cascade="save-update"/>
This all works fine when loading flights with bookings from the
databases, but I do get errors when trying to save new flights + new
bookings.
When I remove the cascade from MainBooking (because it will be saved
as part of the Set) the save of flight throws an exception since
NHibernate thinks that MainBooking is a transient object which might
not be saved. Hence it does not want to include its ID in the flight
table.
When putting the cascade for MainBooking to save-update or even to all
it works fine until I try to delete a flight. I get the error "UPDATE
Flight SET Version = ?, MainBookingID = ? WHERE FlightID = ? AND
Version = ?] ---> System.Data.SqlClient.SqlException: Cannot insert
the value NULL into column 'MainBookingID', table
'WingmanKENew.dbo.Flight'; column does not allow nulls. UPDATE fails"
which puzzles me greatly.
Does anyone know how to map this relationship pattern so a collection
plus an indicator of which one is considered the main/active item
within that collection? I am tempted to let the MainBookingID just be
mapped as a string property rather than a booking class property, but
feel that there should be a better way than exposing the ID.
Suggestions very welcome!
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.