views:

427

answers:

1

Hey there. I have been trying to figure out how to configure the mapping with both NH and FluentNH for days, and I think I'm almost there, but not quite. I have the following problem.

What I need to do is basically map these two entities, which are simplified versions of the actual ones.

Airlines
varchar2(3) airlineCode //PK
varchar2(50)

Aircraft
varchar2(3) aircraftCode //composite PK
varchar2(3) airlineCode //composite PK, FK referencing PK in Airlines
varchar2(50) aircraftName

My classes look like

class Airline
{
    string AirlineCode;
    string AirlineName;
    IList<Aircraft> Fleet;
}

class Aircraft
{
    Airline Airline;
    string AircraftCode;
    string AircraftName;
}

Using FluentNH, I mapped it like so

AirlineMap
    Table("Airlines");
    Id(x => x.AirlineCode);
    Map(x => x.AirlineName);
    HasMany<Aircraft>(x => x.Fleet)
     .KeyColumn("Airline");

AircraftMap
    Table("Aircraft");
    CompositeId()
     .KeyProperty(x => x.AircraftCode)
     .KeyReference(x => x.Airline);
    Map(x => x.AircraftName);
    References(x => x.Airline)
     .Column("Airline");

Using Nunit, I'm testing the addition of another aircraft, but upon calling transaction.Commit after session.Save(aircraft), I get an exception: "System.IndexOutOfRangeException : Invalid index 22 for this OracleParameterCollection with Count=22." The Aircraft class (and the table) has 22 properties.

Anyone have any ideas?

A: 

The problem is that NHibernate is generating a query that doesn't properly match the requirements of one of your tables. I can't quite deduce what is going on based on the information provided. Add ShowSql() onto your OracleClientConfiguration during the construction of your FluentConfiguration object. Then run your test again in NUnit and note that the generated Sql will be in the console window. It should give you a good starting point to see what's actually happening under the hood. If that doesn't help, bring the Sql back for SO to look at.

ddc0660
The only Sql generated is for selecting an Airline.in the NUnit test for CanAddAircraft(), I have IAircraftRepository repo = new AircraftRepository(); Aircraft a = new Aircraft ( Airline = new Airline {AirlineCode = "BAW" } , AircraftCode = "752"}; repo.AddAircraft(a);The repository method is public void AddAircraft(Aircraft a) { using(ISession session = Helper.OpenSession()) { using(ITransaction tx = session.BeginTransAction()) { session.Save(a); tx.Commit(); } } }
Moss

related questions