views:

15

answers:

1

I'm having some issues mapping a complex many to many relationship in fluentnhibernate. I have a legacy db which looks something like this:

Foos: | Id | Foo |

FooBars: | FooId | BarId |

Bars: | Id | Bar | CultureId |

which I am trying to map to the following object model:

class Foo
{
   property virtual int Id { get; set; }
   property virtual string Foo { get; set; }
   property virtual IList<Bar> Bars { get; set; }
}

class Bar
{
   property virtual int Id { get; set; }
   property virtual int CultureId { get; set; }
}

with the mappings:

public class FooMapping : ClassMap<Foo>
{
    public FooMapping()
    {
        Table("foos");
        Id(v => v.Id);
        Map(v => v.Foo);

        HasManyToMany(v => v.Bars)
            .Table("FooBars")
            .ParentKeyColumn("FooId")
            .ChildKeyColumn("BarId")
            .Cascade.All();
    }
}

public class BarMapping : ClassMap<Bar>
{
    public BarMapping()
    {
        Table("bars");
        Id(v => v.Id);
        Map(v => v.Bar);
        Map(v => v.CultureId);
    }
}

The problem is I have multiple Bar's with the same Id for different CultureIds

e.g.

I would have a table that looks like:

Id|Bar|CultureId

1, Hello, 1

1, Bonjour, 2

1, Gutentag, 3

At the moment, the Bars property for the above table will return 3 elements but the Bar property on it will return "Hello" for all three elements (presumably because they all have the same identity). So my question is, how can I either stop this happening or can anyone suggest a way of filtering rows that do not have the correct culture id (note, this is dynamic & based on the current culture)?

A: 

You can't create dynamic where clauses in your mappings. You're going to need to query this collection instead of accessing it via the parent, using a Criteria or HQL query. You could read up on filters, but they still require a query.

James Gregory