简体   繁体   English

为什么这个简单的 NHibernate 一对多关系不是双向的?

[英]Why isn't this simple NHibernate one-to-many relation bidirectional?

I'm trying to set up a simple association in NHibernate (this is the first project in which I want to make use of it from the ground up) - it seems like a simple, contrived, book example, but for some reason I can't get the relation to work bidirectionally.我正在尝试在 NHibernate 中建立一个简单的关联(这是我想从头开始使用它的第一个项目)——这似乎是一个简单、人为的书籍示例,但出于某种原因我可以'没有得到双向工作的关系。

I have a class called Contact which can contain many Addresses.我有一个名为 Contact 的 class 可以包含许多地址。

Here is the simplified Contact class:这是简化的联系人 class:

public class Contact {

// firstName, lastName, etc, etc omitted

// ixContact is the identity value
public virtual int ixContact { get; set; }
public virtual ICollection<Address> addresses { get; set; }

}

and here is Address:这是地址:

public class Address {
       public virtual int ixAddress { get; set; }
       public virtual Contact contact { get; set; }
}

here is the relevant part of the mapping file, Contact.hbm.xml:这是映射文件的相关部分,Contact.hbm.xml:

<id name="ixContact">
    <generator class="hilo" />
</id>

<bag name="Addresses" inverse="true" cascade="save-update">
     <key column="ixContact" />
     <one-to-many class="Address />
</bag>

here is the relevant part of the Address.hbm.xml mapping file:这是 Address.hbm.xml 映射文件的相关部分:

<id name="ixAddress">
    <generator class="hilo" />
</id>

<many-to-one name="contact" class="Contact" column="ixContact" />

Given that setup, I run the following code:鉴于该设置,我运行以下代码:

_configuration = new Configuration();
    _configuration.Configure();
    _configuration.AddAssembly(typeof(Contact).Assembly);

    _sessionFactory = _configuration.BuildSessionFactory();

    _session = _sessionFactory.OpenSession();

    new SchemaExport(_configuration).Execute(false, true, false);

    Contact firstContact = new Contact { firstName = "Joey", middleName = "JoeJoe", lastName = "Shabadoo" };

    using( ITransaction tx = _session.BeginTransaction()) {

           firstContact.Addresses = new List<Address>();
           Address firstAddress = new Address { /* address data */ };

        firstContact.Addresses.Add(firstAddress);

      _session.SaveOrUpdate(firstContact);

      tx.Commit();
    }


    _session.Close();

    _session.Dispose();

Once I run this code, the Contact is inserted into the Contact table just fine, and the Address is also inserted into the Address table, except that the Address's ixContact field is NULL, not associated with the value of the Contact's ixContact field, as I would expect.一旦我运行这段代码,Contact 就可以插入到 Contact 表中,而且 Address 也插入到 Address 表中,只是Address 的 ixContact 字段是 NULL,与 Contact 的 ixContact 字段的值没有关联,因为我会期望。

If I explicitly specify the other side of the relation and say firstAddress.Contact = firstContact , it works fine, but I was under the impression that NHibernate took care of this automagically?如果我明确指定关系的另一端并说firstAddress.Contact = firstContact ,它工作正常,但我的印象是 NHibernate 自动处理这个问题?

If so, what am I doing wrong?如果是这样,我做错了什么? Or do I have to specify RandomContact.Addresses.Add(foo), foo.Contact = RandomContact every time?还是我每次都必须指定RandomContact.Addresses.Add(foo), foo.Contact = RandomContact

Hmm, it looks like you're missing the call _session.Save(firstContact);嗯,看起来你错过了电话_session.Save(firstContact); before calling tx.Commit();在调用tx.Commit();

You do need to set both sides of the relationship explicitly.您确实需要明确设置关系的双方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM