流利的Nhibernate HasMany

时间:2010-06-04 17:37:56

标签: nhibernate fluent-nhibernate

我在理解方面存在差距,我将不胜感激。

当我使用列表创建HasMany关系时,nhibernate在我的子表上创建一个“索引”列。如果我使用连接查询表,它会崩溃“收集时的空索引”

我不理解的是 - 这个“索引”列是如何管理/填充的?

我的相关代码:

家长(产品)

  Class:
       public class License
       {
             ...
             public virtual IList<License>  Licenses { get; set; }
             public Product()
             {
                Licenses = (IList<License>)new List<License>();
             }
       }

   Mapping:

    HasMany<License>(x => x.Licenses)
        .Inverse()
        .Not.LazyLoad()
        .AsList()
        .Cascade.AllDeleteOrphan()
        .ForeignKeyCascadeOnDelete()
        .KeyNullable()
        .KeyColumn("Product_ID")

儿童(执照):

  Class:
       public class License
       {
             ...
             public virtual Product_ID {get; set;}
       }

  Mapping: 


    ...
    References<Product>(x => x.Product_ID)
        .Column("ParentProduct");
    ...

我的加入:

   criteria.Add(Restrictions.Disjunction()
                        .Add(Restrictions.Like("Name", kw))
                        .Add(Restrictions.Like("Description", kw)))
                        .CreateAlias("Licenses", "License",   NHibernate.SqlCommand.JoinType.LeftOuterJoin);

我最终得到了:

[HibernateException: null index column for collection: Test.Licenses]
   NHibernate.Persister.Collection.AbstractCollectionPersister.ReadIndex(IDataReader rs, String[] aliases, ISessionImplementor session) in D:\source\elysium\Elysium.Web\FNH\src\NHibernate\Persister\Collection\AbstractCollectionPersister.cs:703
   NHibernate.Collection.PersistentList.ReadFrom(IDataReader rs, ICollectionPersister role, ICollectionAliases descriptor, Object owner) in D:\source\elysium\Elysium.Web\FNH\src\NHibernate\Collection\PersistentList.cs:120
   NHibernate.Loader.Loader.ReadCollectionElement(Object optionalOwner, Object optionalKey, ICollectionPersister persister, ICollectionAliases descriptor, IDataReader rs, ISessionImplementor session) in D:\source\elysium\Elysium.Web\FNH\src\NHibernate\Loader\Loader.cs:706
   NHibernate.Loader.Loader.ReadCollectionElements(Object[] row, IDataReader resultSet, ISessionImplementor session) in D:\source\elysium\Elysium.Web\FNH\src\NHibernate\Loader\Loader.cs:385
   NHibernate.Loader.Loader.GetRowFromResultSet(IDataReader resultSet, ISessionImplementor session, QueryParameters queryParameters, LockMode[] lockModeArray, EntityKey optionalObjectKey, IList hydratedObjects, EntityKey[] keys, Boolean returnProxies) in D:\source\elysium\Elysium.Web\FNH\src\NHibernate\Loader\Loader.cs:326
   NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) in D:\source\elysium\Elysium.Web\FNH\src\NHibernate\Loader\Loader.cs:453
   NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) in D:\source\elysium\Elysium.Web\FNH\src\NHibernate\Loader\Loader.cs:236
   NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) in D:\source\elysium\Elysium.Web\FNH\src\NHibernate\Loader\Loader.cs:1649
   NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters) in D:\source\elysium\Elysium.Web\FNH\src\NHibernate\Loader\Loader.cs:1568
...

修改

我将集合更改为Bag(或Set),我得到了另一个问题:

Line 30:        public void SetPropertyValues(object target, object[] values)
Line 31:        {
Line 32:            setDelegate(target, values, setterCallback);
Line 33:        }
Line 34: 


Source File: D:\source\Test\FNH\src\NHibernate\Bytecode\Lightweight\AccessOptimizer.cs    Line: 32

Stack Trace:

[InvalidCastException: Specified cast is not valid.]
   (Object , Object[] , SetterCallback ) +409
   NHibernate.Bytecode.Lightweight.AccessOptimizer.SetPropertyValues(Object target, Object[] values) in D:\source\Test\FNH\src\NHibernate\Bytecode\Lightweight\AccessOptimizer.cs:32
   NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(Object entity, Object[] values) in D:\source\Test\FNH\src\NHibernate\Tuple\Entity\PocoEntityTuplizer.cs:292

[PropertyAccessException: Invalid Cast (check your mapping for property type mismatches); setter of Test.License]
   NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(Object entity, Object[] values) in D:\source\Test\FNH\src\NHibernate\Tuple\Entity\PocoEntityTuplizer.cs:296

... 当我追踪它时,似乎问题是我的孩子班级是:

公共类许可证 {

public virtual Guid Id { get; private set; }
...
public virtual Guid Product_ID { get; set; }

}

但是它试图在Product_ID字段中放置Parent类的实例。所以它实际上是在尝试将Product_ID映射到Product类的实例。因此,我最终获得了一个包含许可证数组的产品,并希望让数组中的每个许可证都包含父产品的实例。

2 个答案:

答案 0 :(得分:5)

你不能将Inverse()与有序集合(例如列表)一起使用 - 如果你想使它有序,那么你需要省略Inverse()并从父项继续它

答案 1 :(得分:1)

尝试将其映射为无序集合(包):

HasMany<License>(x => x.Licenses)
    .Inverse()
    .Not.LazyLoad()
    .AsBag()
    .Cascade.AllDeleteOrphan()
    .ForeignKeyCascadeOnDelete()
    .KeyNullable()
    .KeyColumn("Product_ID")

编辑添加:

除了通过References定义多对一对象外,您不应该在子对象上定义外键。许可证应该是:

   public class License
   {
         ...
         public virtual Product {get; set;}
   }

另外,我认为你的意思是产品而且演示是不必要的,因为List<T>实现了IList<T>

   public class Product
   {
         ...
         public virtual IList<License>  Licenses { get; set; }
         public Product()
         {
            Licenses = new List<License>();
         }
   }

希望这有帮助。