映射单向ManyToMany?

时间:2010-10-18 19:56:29

标签: nhibernate fluent-nhibernate

我想将以下公司映射到以下地址。我希望它是单向关系,因为地址类正被其他几个类使用。

当我生成模式时,成功创建了联结表,但是当我尝试保存实体时,联结表中没有任何内容。仅限地址和公司表格。我需要做些什么来完成这项工作?

public CompanyMapping()
{
    Map(x => x.Name);

    HasManyToMany(x => x.Addresses);
}

public AddressMapping()
{
    Id(x => x.Id);

    Map(x => x.AddressLineOne)
        .Not.Nullable();

    Map(x => x.AddressLineTwo)
        .Nullable();

    Map(x => x.PostCode)
        .Not.Nullable();

    Map(x => x.City)
        .Not.Nullable();
}

public class HasManyToManyCascadeConvention : IHasManyToManyConvention
{
    public void Apply(IManyToManyCollectionInstance instance)
    {
        instance.Cascade.All();
        instance.Inverse();
        instance.Key.ForeignKey(Inflector.Underscore("fk_" + instance.EntityType.Name + "_" + instance.ChildType.Name));
    }
}

<class xmlns="urn:nhibernate-mapping-2.2" name="Company" table="`company´">
    <id name="Id">
        <column name="company_id" />
        <generator class="identity" />
    </id>
    <bag table="company_address" lazy="false" cascade="save-update" name="Addresses">
        <key column="company_id" />
        <many-to-many class="Address" column="address_id" />
    </bag>
    <property name="Name">
        <column name="name" />
    </property>
</class>

编辑2010-11-05:如果我像这样添加BillingAddress和DeliveryAddress的映射,我没有任何问题。

References(x => x.BillingAddress)
    .Column("billing_address_id")
    .ForeignKey("fk_account_billing_address")
    .Cascade.All()
    .Fetch.Join();

References(x => x.DeliveryAddress)
    .Column("delivery_address_id")
    .ForeignKey("fk_account_delivery_address")
    .Cascade.All()
    .Fetch.Join();

在rails中我会使用多态关联,我想这意味着NHibernate中的Any关系没有?

1 个答案:

答案 0 :(得分:0)

你需要告诉nHibernate这个系列的哪一侧穿裤子。看看是否有效:

HasManyToMany(x => x.Addresses)
    .Inverse()
    .Cascade.All();