views:

29

answers:

1

Hi guys,

I hope anyone can help. I have to develop up against this third party database and I am kind of stuck with their crappy design. Still, I want to use NHibernate so I will have to jump through hoops.

Simplified, there is this "Event" table that has a relation a "Transportation" table. The transportation table has a composite primary key composed of the field "ID" and "FK_EventID", the latter of course referring back to the Event-record. Each event points to one distinct record in the transportation table, so it is a one-to-one relation really. Both fields are Guids BTW.

Attempting to map this out, this is how I created the classes (leaving out other data fields for simplicity's sake):

   public class FcoEvent : IFcoObject
   {
       public virtual Guid ID { get; set; }

       //public virtual Guid FK_TransportationID { get; set; } //ignore
       public virtual FcoTransportation Transportation { get; set; } 

And:

[Serializable]
public class FcoTransportation : IFcoObject
{
    #region Members

    public virtual Guid ID { get; set; }

    public virtual Guid FK_EventID { get; set; }

In the mapping files I am attempting this (note that I am using many-to-one):

   <class name="FcoLib.FcoEvent, FcoLib" table="FCO_Event">
     <id name="ID" column="ID">
      <generator class="guid" />
     </id>
     <many-to-one name="Transportation" not-found="ignore" cascade="save-update"
                 class="FcoLib.FcoTransportation, FcoLib">
     <column name="FK_TransportationID" />
     <column name="ID" />
    </many-to-one> 

And:

   <class name="FcoLib.FcoTransportation, FcoLib" table="FCO_Transportation">
     <composite-id>
       <key-property name="ID" />
       <key-property name="FK_EventID" />
     </composite-id>

When I try to run this, I get the following exception message:

NHibernate.QueryException: could not resolve property: FK_TransportationID of: FcoLib.FcoEvent

My first hunch was that there may be a spelling error in the field name, but that didn't hold. So now I am completely puzzled and don't know how to proceed. Any help is greatly appreciated. Thnx.


Update

I think I found the source of the error. I had not looked there yet, because I assumed it was a mapping error, but apparently it is a querying error. It happens where I do the query:

fcoEvents = session.CreateCriteria(typeof(FcoEvent))
                    .Add(Restrictions.Eq("ID", eventId))
                    .Add(Restrictions.Eq("FK_TransportationID", transportId))
                    .List<FcoEvent>();

I will look further into this, but obviously I need to query this in a different way...

A: 

Silly me. I have been distracted by some faulty, outdated code. The point was to be able to retrieve the event including the related transport child using the primary key of the event and that simply works. Also it should be possible to retrieve the transport issue with the composite primary key and that can be accomplished with the below code.

    public FcoTransportation GetTransportation(Guid transportId, Guid eventId)
    {
        FcoTransportation transport;

        ISession session = Factory.OpenSession();
        ITransaction tx = session.BeginTransaction();
        try
        {
            transport = session.Get<FcoTransportation>(new FcoTransportation()
               {
                   ID = transportId,
                   FK_EventID = eventId
               });

So this has been a non-issue really. I have just been confused by the whole composite foreign key stuff. I hope I did not waste people's time.

Fedor Steeman

related questions