1

In a project I'm working on, I have four entities(amongst a bunch of others), WorkOrder, Crew, CrewAssignment, and Contractor. Their relationship is like this:

  • A WorkOrder has one Contractor, which can be changed
  • A Crew only ever has one Contractor
  • A CrewAssignment only ever has one Crew
  • A WorkOrder has many CrewAssignments

The problem I'm having is setting up the last part where a WorkOrder can have multiple CrewAssignments. What I want to do is ensure that the WorkOrder.CrewAssignments property only returns the CrewAssignments that have a Crew with the same Contractor as the WorkOrder. Or, less wordy, "where WorkOrder.Contractor == CrewAssignment.Crew.Contractor".

The only thing I've been able to come up with is this, but it throws an exception about the x variable being undefined.

HasMany(x => x.CrewAssignments).KeyColumn("WorkOrderID").Where(x => x.Crew.Contractor == x.WorkOrder.Contractor);

Is doing something like this even possible? Or am I barking up the wrong tree entirely? Google's been failing me all morning with this one. Any ideas?

rossisdead
  • 2,102
  • 1
  • 19
  • 30

1 Answers1

5

I don't know if it could help you but here is my take on in.

In FluentNHibernate you have a method called ApplyFilter that you can set up in your mappings in order to filter on child collections :

HasMany(x => x.CrewAssignments).KeyColumn("WorkOrderID").ApplyFilter<MyFilter>().Cascade.AllDeleteOrphan(); 

Then you can create your filter :

public class MyFilter: FilterDefinition
{
    public MyFilter()
    {
        WithName("contractor").WithCondition("Crew.ContractorId== :contractorId").AddParameter("contractorId", NHibernate.NHibernateUtil.Int32);
    }
}

Then in the implementation of your repository, you invoke the filter :

Sessions.EnableFilter("contractor").SetParameter("contractorId", 1254);

I don't know if this is the best solution but the only one that comes to my mind right now.

Tomasz Jaskuλa
  • 15,723
  • 5
  • 46
  • 73
  • I was reading about that in another post, but I don't see how that'll match up the CrewAssignment.Crew.ContractorID with the WorkOrder.ContractorID. It doesn't look like it incorporates the Crew entity in there. – rossisdead Dec 16 '11 at 15:59
  • Trying that, no matter what I use in the WithName argument, I get a "Duplicated filter-def name" exception when FluentConfiguration.BuildSessionFactory is called. – rossisdead Dec 16 '11 at 16:08
  • I modified the answer. Look at the WithCondition method call in the filter and Crew.ContractorId as parameters. For the exception I'm not sure. It seems like the filter was definied twice with the same name. – Tomasz Jaskuλa Dec 16 '11 at 16:09
  • Thanks Thomas, I've been trying your example, but I just can't get past the duplicate filter issue :/ – rossisdead Dec 16 '11 at 17:59
  • Maybe you're encountering that issue: http://stackoverflow.com/questions/7981519/fluentnhibernate-filterdefinition-not-being-applied – Tomasz Jaskuλa Dec 16 '11 at 19:48
  • I wish that were the case(cause then I could fix it!), but all our entity classes are all their own POCOs. – rossisdead Dec 16 '11 at 21:07