Breaking changes with NHibernate 4 upgrade

前端 未结 3 910
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-05 21:53

I can see what\'s new and fixed in NHibernate 4.0

I would like to know if anyone has had issue with hbm mappings upgrading from NHibernate 3 to 4?

I fear tha

3条回答
  •  太阳男子
    2021-01-05 22:32

    We were experiencing the same issue with a rather large QueryOver - Cannot simultaneously fetch multiple bags, with Nhibernate 4 and FluentNhibernate mappings.

    The solution was to, on our FluentMaps, to set AsSet() (according to our backing fields) which in the end solved the issue.

    As per request in the comment, here is a small sample of our mappings before and after the exception:

    This is a very simplified showcase of our classes which caused the Cannot simultaneously fetch multiple bags. The abstract Entity class belongs to the S#Arp lite architecture. Before the changes it looked something like this

    public class OrderHeader : Entity
    {
        public virtual IList Items { get; set; }
    }
    
    public class OrderItem : Entity
    {
        public virtual decimal Price {get; set;}
        public virtual string ItemNumber {get; set;}
        public virtual OrderHeader Header {get; set;}
    }
    
    public class OrderHeaderMap : ClassMap
    {
        Id( e => e.Id).GeneratedBy.Native();
        HasMany(e => e.OrderItems).Inverse();
    }
    
    public class OrderItemMap : ClassMap
    {
        Id(e => e.Id).GeneratedBy.Native();
        References(e => e.Header).Not.Nullable();
    }
    

    As you can see the OrderHeader has an IList of items. Changing this to

    public class OrderHeader : Entity
    {
        public virtual ISet Items { get; set; } // ISet here
    }
    
    public class OrderItem : Entity
    {
        public virtual decimal Price {get; set;}
        public virtual string ItemNumber {get; set;}
        public virtual OrderHeader Header {get; set;}
    }
    
    public class OrderHeaderMap : ClassMap
    {
        Id( e => e.Id).GeneratedBy.Native();
        HasMany(e => e.OrderItems).Inverse();
    }
    
    public class OrderItemMap : ClassMap
    {
        Id(e => e.Id).GeneratedBy.Native();
        References(e => e.Header).Not.Nullable().AsSet(); // Explicit AsSet
    }
    

    So an ISet and the explicit AsSet() on the mapping made the issue go away. This code snippet is very simplified and we had multiple entities in the QueryOver with HasMany() IList - when all were updated to the ISet, it worked properly.

提交回复
热议问题