English 中文(简体)
NHibernate: bidirectional manytoone problem
原标题:NHibernate: bidirectional many-to-one problem

我在恩贾梅纳有双向关系:

<class name="Child" table="Children">
  <many-to-one name="Parent" column="ParentId" not-null="true" />
</class>

<class name="Parent">
  <set name="Children" lazy="true" table="Children" cascade="all">
    <key column="ParentId" not-null="true" />
    <one-to-many class="Child" />
  </set>
</class>

How do I save it without setting inverse="true" and setting Parent property on Child?
I do not want to do this because it does not make much sense from the POCO perspective.

Alternatively, is it possible to intercept Add called on NHibernate proxy collection (Children)?
In that case, I would just put the Parent setting logic here.

问题回答

除非你愿意使外国钥匙失去效力,并接受随后更新的插入,否则,这种改观是解放国双向的。

我确实普遍实施你可以采用的这种模式,......它提供了概念的证明;它可能有用或不有用,这取决于你如何看待它,因为这种做法会打破POCO的做法,但......好是:

public interface IHaveParent<T>
{
    T Parent { get; set; }
}

public interface IHaveMany<T>
{
    ICollection<T> Children { get; }
}

public static class OneToManyHelper
{
    public static void AddChild<TParent, TChild>(this TParent parent,
                                                 TChild child)
    where TChild : IHaveParent<TParent>
    where TParent : IHaveMany<TChild>
    {
        parent.Children.Add(child);
        child.Parent = parent;
    }
}

如果有的话,你可以就任何家长制定<条码>。

拦截Add号电话的问题是,你总是需要使用特殊方法(这同样不是POCO)即时收。





相关问题
nHibernate one-to-many inserts but doesnt update

Instead of getting into code, I have a simple question. Default behavior for a simple one-to-many is that it inserts the child record then updates the foreign key column with the parent key. Has ...

How Do I copy an existing nhibernate object as a new object?

I a persisted NHibernate object that I would like to repersist as a new entity. How do I get NHibernate to save this object as if it was a new? I am thinking I might create a session interceptor to ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

NHibernate Search in a List using ICriteria

I have my class X : public class ClassX { public virtual IList<ClassY> ListY { get; set; } ... } My ClassX mapping (using Fluent) ... HasMany<ClassX>(x => x.ListY ) ....

热门标签