简体   繁体   English

使用NHibernate的Child-Parent关系

[英]Child-Parent relationship using NHibernate

I'm trying to figure out how to make child-parent relationships using NHibernate. 我正试图弄清楚如何使用NHibernate建立子父关系。 I have two classes Foo and Bar. 我有两个班Foo和Bar。 Foo have a collection of Bars and Bar have reference to parent Foo. Foo有一个Bars和Bar的集合,引用了父Foo。

public class Foo
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual List<Bar> Children { get; set; }  
}

public class Bar
{
    public virtual int Id { get; set; }
    public virtual string Description { get; set; }
    public virtual Foo Parent { get; set; }
}

The mapping of this classes are: 这些类的映射是:

  <class name="Foo" table="foos">
    <id name="Id" column="id">
      <generator class="identity"/>
    </id>
    <property name="Name" column="name" />
    <set name="Children" inverse="true" lazy="true">
      <key column="fooId"/>
      <one-to-many class="Bar"/>
    </set>
  </class>

  <class name="Bar" table="bars">
    <id name="Id" column="id">
      <generator class="identity"/>
    </id>
    <property name="Description" column="description" />
    <many-to-one name="Parent" column="fooId"/>
  </class>

This is sesion factory creation: 这是sesion工厂创建:

if (_sessionFactory == null)
{
    var configuration = new Configuration();
    configuration.Configure();
    configuration.AddAssembly(typeof(Foo).Assembly);
    _sessionFactory = configuration.BuildSessionFactory();
}

And here is how I'm adding new Foo: 这是我如何添加新的Foo:

        Foo foo = new Foo { Name = "name1" };
        foo.Children = new List<Bar>();
        foo.Children.Add(new Bar { Description = "desc1" });
        foo.Children.Add(new Bar { Description = "desc2" });

        using (ISession session = NHibirnateHelper.OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Save(foo);
                transaction.Commit();
            }

When calling Save(foo) method I have an exception "Invalid Cast (check your mapping for property type mismatches); setter of Test.Foo". 当调用Save(foo)方法时,我有一个异常“Invalid Cast(检查你的映射是否有属性类型不匹配); setter of Test.Foo”。 With iinere exception: ""Unable to cast object type \\"NHibernate.Collection.Generic.PersistentGenericSet 1[Test.Bar]\\" к типу \\"System.Collections.Generic.List 1[Test.Bar]\\"". 使用iinere异常:“”无法转换对象类型\\“NHibernate.Collection.Generic.PersistentGenericSet 1[Test.Bar]\\" к типу \\"System.Collections.Generic.List 1 [Test.Bar] \\”“。

What I'm doing wrong? 我做错了什么?

Try changing public virtual List<Bar> Children { get; set; } 尝试更改public virtual List<Bar> Children { get; set; } public virtual List<Bar> Children { get; set; } public virtual List<Bar> Children { get; set; } to public virtual ISet<Bar> Children { get; set; } public virtual List<Bar> Children { get; set; }public virtual ISet<Bar> Children { get; set; } public virtual ISet<Bar> Children { get; set; } public virtual ISet<Bar> Children { get; set; } . public virtual ISet<Bar> Children { get; set; } ISet can be found in Iesi.Collections.Generic.ISet<> . ISet可以在Iesi.Collections.Generic.ISet<>找到。

You could even try using IList instead of List and see if it works.Avoid using concrete classes for the collections and try using interfaces so that Nhibernate could inject its own concrete implementations. 您甚至可以尝试使用IList而不是List ,看看它是否有效。避免使用集合的具体类并尝试使用接口,以便Nhibernate可以注入自己的具体实现。

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

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