开发者

fluent nhibernate - many-to-many relationship mapping on same entity

I am having a problem trying to map out a many-to-many relationship , where both sides of the relationship reference the same entity. I am using Fluent NHibernate and NH3.1.

Basically, the scenario is this - I have a category, which can have multiple parents. Thus, a category has multiple other categories as parents, as well as multiple other categories as its children.

HasManyToMany(x => x.ParentCategories).AsBag().Table("parentcategorychildren").ParentKeyColumn("ChildID").ChildKeyColumn("ParentID").Cascade.SaveUpdate();
HasManyToMany(x => x.ChildrenCategories).AsBag().Table("parentcategorychildren").ParentKeyColumn("ParentID").ChildKeyColumn("ChildID").Inverse();

However, when I try to build the factory, I get the following error:

The relationship Category.ChildrenCategories to Category.ChildrenCategories has Inverse specified on both sides. Remove Inverse from one side of the relationship.

What I am finding strange is why is it mentioning 'Category.Children开发者_如何学运维Categories' to Category.ChildrenCategories, as opposed to ParentCategories?

Any help would be greatly appreciated !

I just created a bounty for this, because it's important enough to me. Please, I'm not interested in "you can't do this" as an answer.


This is most likely a FNH bug and it is most likely already fixed in the latest FNH source code. There is no problem when using FNH1.0 and NH2.1. Equivalent HBM mapping works well in FNH1.2 and NH3.1:

<bag name="ParentCategories" cascade="all" table="parentcategorychildren">
    <key column="ChildID" />
    <many-to-many column="ParentID" class="Category" />
</bag>

<bag name="ChildrenCategories" inverse="true" table="parentcategorychildren">
    <key column="ParentID" />
    <many-to-many column="ChildID" class="Category" />
</bag>

EDIT: After digging in FNH source code I found a workaround. Let's say, your configuration looks like this:

.Mappings(m => {
    m.FluentMappings.AddFromAssemblyOf<Category>();
})

The unlucky code can be suppressed by this configuration:

.Mappings(m => {
    var persistenceModel = new PersistenceModel();
    persistenceModel.AddMappingsFromAssembly(typeof(Category).Assembly);
    persistenceModel.ValidationEnabled = false; // this makes the trick
    m.UsePersistenceModel(persistenceModel);
})


This is an issue with Fluent NHibernate 2.1's validation/pairing of relationships. FNH pairs up relationships and then validates that only one side of the relationship has .Inverse() specified. Because both references (parent/child) are to the same class they are both candidate matches when pairing. In that case, FNH matches on name similarity. Consequently, they each get paired with themselves rather than each other. So then placing .Inverse() on either one triggers the validation (both sides of the pair are the same relationship which is inverse).

It should be possible to correct this using the OverrideBiDirectionalManyToManyPairing() method on the FluentMappingsContainer. In theory, that would allow you to explicitly pair the child and parent relationships. However, in FNH 2.1 there is a bug, and the override callback is never called. (The callback value is captured before it can be set by the method).

As a work around, you can disable all validation in FNH. There are only two validations. First, that both sides of a relationship don't have .Inverse(). Second, that an Id is mapped on each entity. The cleanest way I have found to disable validation is:

.Mappings(m => {
    var persistenceModel = new PersistenceModel() { ValidationEnabled = false };
    m.UsePersistenceModel(persistenceModel)
     .FluentMappings.AddFromAssemblyOf<Category>();
})

This approach allows you to disable validation but still use the full expressiveness of the FluentMappings configuration.


Yes it seemed to me that it was most likely a bug in FNH, as I tried it directly with NHibernate without using Fluent NH and it worked. However, as I had already setted up a system using FNH I couldn't just revert to not using it.

What I did was I created like a 'class-in-the-middle' myself for the many-to-many relationship, which normally is generated automatically. I created a ContentPage_ChildLink page, which linked the Parents and Children categories. This allowed me to work with FNH and work around the problem :)

Basically this ContentPage_ChildLink would have two fields, ChildID and ParentID. I could then set the 'Inverse' relationships seperately, without any problem.

The problem with FNH seemed to be when you have a many-to-many relationship, which both sides are the same class, the only case I can think of is a hierarchy structure which allows for multiple parents.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜