views:

300

answers:

4

Does anybody know how I would map an entity with two many-to-many collections of the same child type.

My database structure is this....

The "normal" relationship will be....

tbl_Parent
  col_Parent_ID

tbl_Parent_Child_Xref
   col_Parent_ID
   col_Child_ID

tbl_Child
   col_Child_ID


The alternative relationship is...

tbl_Parent
  col_Parent_ID

tbl_Include_ParentChild_Xref
   col_Parent_ID
   col_Child_ID

tbl_Child
   col_Child_ID


The entity and mapping look like this...

public partial class ParentEntity : AuditableDataEntity<ParentEntity>
{
  public virtual IList<ChildEntity> Children { get; set; }
  public virtual IList<ChildEntity> IncludedChildren { get; set; }
}

public partial class ParentMap : IAutoMappingOverride<ParentEntity>
{
    public void Override(AutoMapping<ParentEntity> mapping)
    {
     mapping.Table("tbl_Parent");

     mapping.HasManyToMany(x => x.Children)
     .Table("tbl_Parent_Child_Xref")
     .ParentKeyColumn("col_Parent_ID")
     .ChildKeyColumn("col_Child_ID")
     .Inverse()
     .Cascade.All();

     mapping.HasManyToMany(x => x.IncludedChildren)
     .Table("tbl_Include_ParentChild_Xref")
     .ParentKeyColumn("col_Parent_ID")
     .ChildKeyColumn("col_Child_ID")
     .Inverse()
     .Cascade.All();
    }
}


The error that I'm getting is "System.NotSupportedException: Can't figure out what the other side of the many-to-many property 'Children' should be."

I'm using NHibernate 2.1.2, FluentNhibernate 1.0.

A: 

You know I'm just shooting in the dark here, but it almost sounds like your ChildEntity class isn't known by Hibernate .. that's typically where I've seen that sort of message. Hibernate inspects your class and sees this referenced class (ChildEntity in this case) that id doesn't know about.

Maybe you've moved on and found the issue at this point, but thought I'd see anyway.

BryanD
If I comment out one of the lists of children, it works ok, so the mapping knows about the child entity. Thanks though.
Rob Gibbens
A: 

To my great pity, NHibernate cannot do that. Consider using another ORM.

Any documentation to back up your claim that it can't do that? A link to a "known issues" page or "feature requests" - etc? I'm fairly certain that it _can_ do it.
Daniel Schilling
A: 

Fluent is confused because you are referencing the same parent column twice. That is a no-no. And as far as I can tell from the activity i have seen, a fix is not coming any time soon.

You would have to write some custom extensions to get that working, if it is possible.

Sky Sanders
It's possible to reference the same parent column in a many-to-many when the related types are different. Is this only a limitation when both relations are to the same type?
Jamie Ide
I haven't run into that yet. But have run into the arg out of range and/or missing parameter value when I had a junction table and did not 'Not.Insert().Not.Update()' on the fields which is what taught me to only make one mapping element active. Sorry not more help, just learning nh/fnh myself.
Sky Sanders
+1  A: 

It seems FNH is confused because you seem to map the same object (ChildEntity) to two different tables, if I'm not mistaken.

If you don't really need the two lists to get separated, perhaps using a discriminating value for each of your lists would solve the problem. Your first ChildEntity list would bind to the discriminationg value A, and you sesond to the discriminating value B, for instance.

Otherwise, I would perhaps opt for a derived class of your ChildEntity, just not to have the same name of ChildEntity.

IList<ChildEntity> ChildEntities
IList<IncludedChildEntity> IncludedChildEntities

And both your objects classes would be identitical.

If you say it works with NH, then it might be a bug as already stated. However, you may mix both XML mappings and AutoMapping with FNH. So, if it does work in NH, this would perhaps be my preference. But think this workaround should do it.

Will Marcouiller