views:

250

answers:

1

Hey Guys I would very much appreciate some help with the following. We're using fluent to control the mappings for nhibernate and its all gone well so far. We've basically got a scheduled based CMS system and I'm having problems using the HasMany mapping to exclude the non-live child categories.

So we have the following Data Tables, simplified slightly which map to sensible BO's.

[Category] Id Name Parent_Id Schedule_Id

[Schedule] Id IsPaused (Bit 0/1) StartDate StopDate

The CategoryMap looks a bit like this (vb.net sorry!).

Public Sub New()
    Id(Function(x) x.Id)
    Map(Function(x) x.Name)
    HasMany(Function(x) x.Children).Inverse().KeyColumn("Parent_id").Cascade.All()
    References(Function(x) x.Parent)
    References(Function(x) x.Schedule).ForeignKey("Schedule_id").Fetch.Join().Nullable()
End Sub

What I would like to do is add a filter on the HasMany mapping, but can't seem to get Where to work as I need to.

Could someone please point me in the right direction?

Many Thanks,

Mike.

A: 

You don't show the child class so I'm not sure what the filter might be. Assuming you want to filter on an IsActive bit column the mapping would be:

Public Sub New()
    Id(Function(x) x.Id)
    Map(Function(x) x.Name)
    HasMany(Function(x) x.Children).Inverse().KeyColumn("Parent_id").Cascade.All()
        .Where("IsActive = 1")
    References(Function(x) x.Parent)
    References(Function(x) x.Schedule).ForeignKey("Schedule_id").Fetch.Join().Nullable()
End Sub

The key point is that the Where clause contains a snippet of SQL not HQL, so you have to use database field names instead of property names.

Jamie Ide
I did try that but the SQL generated resolves to (children0_.IsPaused = 0) not (schedule1_.IsPaused = 0). But it led me to use schedule1_.myfields. Thanks man.
Mike Miller
I'm not sure I follow that. You asked for a filter on the HasMany. You shouldn't be using the NHibernate aliased table names (schedule1_) in the Where constraint.
Jamie Ide
I'm trying to filter on a property of the Category in the HasMany mapping, the property is called Schedule. Sorry probably should have explained that category joins back onto itself. So what I've ended up with is - HasMany(Function(x) x.Children)....where("((schedule1_.id is null) or (schedule1_.isPaused=0 and getDate() between coalesce(schedule1_.golivedate,getdate()) and coalesce(schedule1_.stoplivedate,getdate())))")If i remove schedule1_ it replaces it with the alias for the category table, if I try schedule it can't resolve it to the alias used in the query.
Mike Miller
Thanks for the help, I'm going to try and neaten it up and read some more documentation.
Mike Miller