1

I have an object "Location" which has a List of sublocations. My object and mapping are looking like this

    private int _id;
    private string _name;
    private IList<Location> _subLocations;
    private IList<Stock> _stockList;
    private Location _parent;
    private bool _isActive;
    private bool _recommend;

    public virtual IList<Location> SubLocations
    {
        get
        {
            if (_subLocations == null)
            {
                _subLocations = new List<Location>();
            }

            return _subLocations;
        }
        set
        {
            _subLocations = value;
            OnPropertyChanged("SubLocations");
        }
    }

    // more properties ...

And my mapping looks like this:

public class LocationMap:ClassMap<Location>
{
    public LocationMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.IsActive);
        References(x => x.Parent);

        HasMany(x => x.SubLocations).Where(x => x.IsActive == true);
        HasMany(x => x.StockList).Where(x => x.IsActive == true);            

        Table("tbl_locations");

    }
}

I'm also 100% sure that the location objects have IsActive = true

But when I get a Location object the SubLocations list is always empty. Does anybody knows how this is behaving? Or any idea to debug such an issue?

EDIT My database looks like this:

   Id   Name          IsActive  Parent_id
   1    Magazijn A       1     NULL
   2    Magazijn B       1     NULL
   3    Gang A           1      2
   4    Rek B            1          3

Query This is my query to get all parent locations

    public IList<Location> GetAllParentLocations()
    {
        var result = NHibernateHelper.Session.CreateQuery("from Location l fetch all properties where l.Parent is null and l.IsActive = true").List<Location>();
        return (List<Location>)result ?? new List<Location>();
    }

Generated SQL

      NHibernate: select location0_.Id as Id15_, location0_.Name as Name15_,  location0_.IsActive as IsActive15_, location0_.Parent_id as Parent4_15_ from tbl_locations location0_ where (location0_.Parent_id is null) and location0_.IsActive=1
NHibernate: SELECT sublocatio0_.Location_id as Location5_1_, sublocatio0_.Id as Id1_, sublocatio0_.Id as Id15_0_, sublocatio0_.Name as Name15_0_, sublocatio0_.IsActive as IsActive15_0_, sublocatio0_.Parent_id as Parent4_15_0_ FROM tbl_locations sublocatio0_ WHERE  (sublocatio0_.IsActive = 1) and sublocatio0_.Location_id=@p0;@p0 = 1 [Type: Int32 (0)]
         NHibernate: SELECT sublocatio0_.Location_id as Location5_1_, sublocatio0_.Id as Id1_, sublocatio0_.Id as Id15_0_, sublocatio0_.Name as Name15_0_, sublocatio0_.IsActive as IsActive15_0_, sublocatio0_.Parent_id as Parent4_15_0_ FROM tbl_locations sublocatio0_ WHERE  (sublocatio0_.IsActive = 1) and sublocatio0_.Location_id=@p0;@p0 = 42 [Type: Int32 (0)]
NHibernate: SELECT sublocatio0_.Location_id as Location5_1_, sublocatio0_.Id as Id1_, sublocatio0_.Id as Id15_0_, sublocatio0_.Name as Name15_0_, sublocatio0_.IsActive as IsActive15_0_, sublocatio0_.Parent_id as Parent4_15_0_ FROM tbl_locations sublocatio0_ WHERE  (sublocatio0_.IsActive = 1) and sublocatio0_.Location_id=@p0;@p0 = 60 [Type: Int32 (0)]

In the query it does the where clause on "Location_id" column. But It has to use the Parent_id column. Not sure where this Location_id column is coming from...

koala
  • 1,544
  • 1
  • 20
  • 33
  • 1
    Have you looked at the SQL being generated? Are you getting the SQL you should? Look at NHProf or enable log4net – Rippo Sep 03 '13 at 11:49
  • I've looked at the output and I see that the query doesn't get the sublocations, but I don't know why. (I've edited my post) – koala Sep 03 '13 at 13:17
  • when you hit a breakpoint and watch `GetAllParentLocations().SubLocations` what happens? do you get an exception or is lazy loading triggered? – Rippo Sep 03 '13 at 13:24
  • Nothing really happens. But I've looked at my query again and I see that my table also has as an "Location_Id" column. And it does a "where" on Location_id instead of Parent_id. But In my mapping I'm using Parent...I have no idea where this "Location_id" column is coming from – koala Sep 03 '13 at 13:40
  • I've added my solution: HasMany(x => x.SubLocations).KeyColumns.Add("Parent_Id").Where(x => x.IsActive == true); seems to solve it. – koala Sep 03 '13 at 13:51

2 Answers2

1

did you turned off lazy loading? and I suggest you to have a look at this link about lazy loading Is Lazy Loading required for nHibernate?

Community
  • 1
  • 1
Sai
  • 159
  • 1
  • 9
  • Lazy loading is active, and it works in all other cases except for my Location object which also has a Parent Location object. That's the only difference, but I can't figure out why this is behavg like this – koala Sep 03 '13 at 13:20
1

The following link solved my problem:

Fluent NHibernate HasMany Foreign Key Mapping Problem

I've changed my mapping to:

HasMany(x => x.SubLocations).KeyColumns.Add("Parent_Id").Where(x => x.IsActive == true);
cs95
  • 379,657
  • 97
  • 704
  • 746
koala
  • 1,544
  • 1
  • 20
  • 33