且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

流利的NHibernate多对多映射3类

更新时间:2022-10-23 23:00:33

我相信你需要给NH一些关于关系的更多细节。
试试在A的映射和B的映射中写出完整的定义:

$ p $ HasManyToMany(a => a.ClassesB)
.AsSet()
.WithTableName(TBL_A_TO_B)
.WithParentKeyColumn(A_ID)
.WithChildKeyColumn(B_ID)
。 Cascade.All();


First, I'm using NHibernate 3.2 and FluentNHibernate 1.3. I have 3 clasess:

public class ClassA
{
    public virtual Int32 Id{ get; }
    ....
    public ICollection<ClassB> ClassesB { get; }
    public ICollection<ClassC> ClassesC { get; }
}

public class ClassB
{
    public virtual Int32 Id{ get; }
    ....
    public ICollection<ClassA> ClassesA { get; }
    public ICollection<ClassC> ClassesC { get; }
}

public class ClassC
{
    public virtual Int32 Id{ get; }
    ....
    public ICollection<ClassA> ClassesA { get; }
    public ICollection<ClassB> ClassesB { get; }
}

I mapped them with this code:

public class ClassAMap : ClassMap<ClassA>
{
    public ClassAMap()
        : base()
    {
        Id(m => m.Id);
        ....

        HasManyToMany<ClassB>(m => m.ClassesB).Cascade.SaveUpdate().AsSet().ReadOnlyAccess();
        HasManyToMany<ClassC>(m => m.ClassesC).Cascade.SaveUpdate().AsSet().ReadOnlyAccess();
    }
}

public class ClassBMap : ClassMap<ClassB>
{
    public ClassBMap()
        : base()
    {
        Id(m => m.Id);
        ....

        HasManyToMany<ClassA>(m => m.ClassesA).Cascade.SaveUpdate().AsSet().ReadOnlyAccess();
        HasManyToMany<ClassC>(m => m.ClassesC).Cascade.SaveUpdate().AsSet().ReadOnlyAccess();
    }
}

public class ClassCMap : ClassMap<ClassC>
{
    public ClassCMap()
        : base()
    {
        Id(m => m.Id);
        ....

        HasManyToMany<ClassA>(m => m.ClassesA).Cascade.SaveUpdate().AsSet().ReadOnlyAccess();
        HasManyToMany<ClassB>(m => m.ClassesB).Cascade.SaveUpdate().AsSet().ReadOnlyAccess();
    }
}

Each class has a collection to the other 2 classes, don't ask why, but I need to map this to a database. Nhibernate generates 3 tables, 2 of them with 2 fields and the third one with the 3 id fields from all the classes, o this third table just 2 fields are part of a Primary Key. I also tryed to asign the table name for the 3 relationships, and NHibernate generates just one table with the 3 fields, but again, only 2 of them are part of the Primery Key. I'm assuming that the 3 field must be part of the primary key given this type of mapping.

The problem is that I can't add items to either collection (I also tried adding the item to the other side class, eg: when I add ClassB to ClassA also add ClassA to ClassB to maintaing the relationship), when I tried to save to database, It throws a FOREING KEY exception and all of the field are set correctly.

My question is, what is the best way to map these three classes? Do I need to do anything else? Am I forgeting something?

I believe you need to give NH some more details about the relations.
Try Writing the full definition in A's mapping and B's mapping:

HasManyToMany(a => a.ClassesB)
     .AsSet()
     .WithTableName("TBL_A_TO_B")
     .WithParentKeyColumn("A_ID")
     .WithChildKeyColumn("B_ID")
     .Cascade.All();