1
votes

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"));
1
Does wellHeader.AFEs return the same 4 entities? - mxmissile
Hard to be sure without seeing your table structure, class structure and mappings. Are you sure you want the HasMany to be inverted to a many-to-one? I don't see a property referencing back to the parent to support this relationship. - Paul Turner
Yes, wellHeader.AFEs always returns the same 4. Even if I add uncomment the afe in the query, I should only return 1 AFE, but it still returns 4. I think I threw the inverse on there just because of a sample I found. Same result if I leave the inverse off. - SnyderJK
You could check to see if these 4 are actually the same reference and remove them by using .Distinct() on your list. - Egor Pavlikhin

1 Answers

0
votes

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.