views:

51

answers:

1

I'm fairly new to nhibernate and fluent nhibernate, I have an issue with associating a collection to a query. In the database I have a well and have 4 associated AFEs. The problem I'm having is that the collection of AFEs is not populating correctly. No matter what I do, I get 4 AFEs in the collection but they are all the same object. Is there anything obvious that I am doing wrong.

Thanks

PS. I don't have change access to the database that I'm hitting so I can't change the db to make this a true FK.

Parent

    public WellHeaderMap()
    {
        Table("Well");
        Id(x => x.PropertyNumber, "WELL_NUMBER");
        Map(x => x.PropertyID, "PROPERTY_ID");
        Map(x => x.Name, "WELL_NAME");

        //AFEs is a IList<AFE>
        HasMany(x => x.AFEs).Inverse().KeyColumn("Property_ID").PropertyRef("PropertyID").Fetch.Join();
    }

Collection

    public AFEMap()
    {
        Table("AFE");
        Id(x => x.PropertyID, "PROPERTY_ID");
        Map(x => x.AFETypeID, "AFE_TYPE_CODE");
        Map(x => x.AFENumber, "AFE_NUMBER");
        Map(x => x.IsDeleted, "DELETED_IND");
    }

Query

        var wellSearchCriteria = _session.CreateCriteria<WellHeader>()
            .CreateAlias("AFEs", "afe")
            .Add(Restrictions.Eq("PropertyNumber", id.ToString()))
            //.Add(Expression.Eq("afe.AFETypeID", "01"))
            //.Add(Expression.Eq("afe.IsDeleted", "N"));
A: 

I think you might have your WellHeader Id wrong, currently you have:

Id(x => x.PropertyNumber, "WELL_NUMBER");
Map(x => x.PropertyID, "PROPERTY_ID");

Probably should be:

Id(x => x.PropertyID, "PROPERTY_ID");
Map(x => x.PropertyNumber, "WELL_NUMBER");

The PropertyNumber and PropertyId were switched. However without seeing your schema, its hard to tell.

mxmissile
technically, it's not a primary key on the table itself, I was just arbitrarily setting the Id to assist with mapping to some of my own tables.
SnyderJK
If PropertyID is not a PK, is it possible you have multiple Wells in the with the same PROPERTY_ID?
mxmissile
There is a unique constraint on PROPERTY_ID, and i did a query just to make sure and there are no property ids repeated.
SnyderJK
Does WELL_NUMBER also have a unique constraint?
mxmissile
yes, it also unique. I know, I know, if I would have created the database, I would have done it different. That's the joy of vendored applications :)
SnyderJK
Can you try swapping the two around like shown in my answer, just as a quick test?
mxmissile
Then see if WellHeader.AFEs returns the same 4. Leaving your query out of the picture.
mxmissile
Same result, I had to take off the PropertyRef("PropertyID") (Got a "could not resolve property: AFEs of: Domain.WellHeader" QueryException. But I still get the same 4.
SnyderJK