locked
NHibernate - Could not compile the mapping document RRS feed

  • Question

  • User-626088549 posted

    Hey everyone,

    I'm working through a tutorial which uses an older version of NHibernate. I'm trying to use version 3.1.0. However, I get the following error: Could not compile the mapping document: Agathas.Storefront.Repository.NHibernate.Mapping.Basket.hbm.xml. The inner exception of the error is: Could not find the property 'Items', associated to the field '_items', in class 'Agathas.Storefront.Model.Basket.Basket'.

    Here's the full stack trace:

    NHibernate.MappingException was unhandled by user code
    Message=Could not compile the mapping document: Agathas.Storefront.Repository.NHibernate.Mapping.Basket.hbm.xml
    Source=NHibernate
    StackTrace:
    at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:line 342
    at NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:line 530
    at NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:line 499
    at NHibernate.Cfg.Configuration.ProcessMappingsQueue() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:line 1832
    at NHibernate.Cfg.Configuration.AddDocumentThroughQueue(NamedXmlDocument document) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:line 1823
    at NHibernate.Cfg.Configuration.AddXmlReader(XmlReader hbmReader, String name) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:line 1816
    at NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:line 632
    at NHibernate.Cfg.Configuration.AddResource(String path, Assembly assembly) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:line 670
    at NHibernate.Cfg.Configuration.AddAssembly(Assembly assembly) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:line 763
    at NHibernate.Cfg.Configuration.AddAssembly(String assemblyName) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:line 746
    at Agathas.Storefront.Repository.NHibernate.SessionFactory.Init() in C:\AspPatterns\Agathas.Storefront\Agathas.Storefront.Repository.NHibernate\SessionFactory.cs:line 18
    at Agathas.Storefront.Repository.NHibernate.SessionFactory.GetSessionFactory() in C:\AspPatterns\Agathas.Storefront\Agathas.Storefront.Repository.NHibernate\SessionFactory.cs:line 28
    at Agathas.Storefront.Repository.NHibernate.SessionFactory.GetNewSession() in C:\AspPatterns\Agathas.Storefront\Agathas.Storefront.Repository.NHibernate\SessionFactory.cs:line 35
    at Agathas.Storefront.Repository.NHibernate.SessionFactory.GetCurrentSession() in C:\AspPatterns\Agathas.Storefront\Agathas.Storefront.Repository.NHibernate\SessionFactory.cs:line 46
    at Agathas.Storefront.Repository.NHibernate.Repositories.Repository`2.FindAll() in C:\AspPatterns\Agathas.Storefront\Agathas.Storefront.Repository.NHibernate\Repositories\Repository.cs:line 43
    at Agathas.Storefront.Services.Implementations.ProductCatalogService.GetAllCategories() in C:\AspPatterns\Agathas.Storefront\Agathas.Storefront.Services\Implementations\ProductCatalogService.cs:line 90
    at Agathas.Storefront.Controllers.Controllers.ProductCatalogBaseController.GetCategories() in C:\AspPatterns\Agathas.Storefront\Agathas.Storefront.Controllers\Controllers\ProductCatalogBaseController.cs:line 25
    at Agathas.Storefront.Controllers.Controllers.HomeController.Index() in C:\AspPatterns\Agathas.Storefront\Agathas.Storefront.Controllers\Controllers\HomeController.cs:line 26
    at lambda_method(Closure , ControllerBase , Object[] )
    at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
    at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
    at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a()
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
    InnerException: NHibernate.PropertyNotFoundException
    Message=Could not find the property 'Items', associated to the field '_items', in class 'Agathas.Storefront.Model.Basket.Basket'
    Source=NHibernate
    AccessorType=_items
    PropertyName=Items
    StackTrace:
    at NHibernate.Properties.FieldAccessor.GetGetter(Type theClass, String propertyName) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Properties\FieldAccessor.cs:line 70
    at NHibernate.Util.ReflectHelper.GetGetter(Type theClass, String propertyName, String propertyAccessorName) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Util\ReflectHelper.cs:line 112
    at NHibernate.Util.ReflectHelper.ReflectedPropertyClass(Type theClass, String name, String access) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Util\ReflectHelper.cs:line 150
    at NHibernate.Cfg.XmlHbmBinding.CollectionBinder.GetPropertyType(Type containingType, String propertyName, String propertyAccess) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\XmlHbmBinding\CollectionBinder.cs:line 892
    at NHibernate.Cfg.XmlHbmBinding.CollectionBinder.BindCollection(ICollectionPropertiesMapping collectionMapping, Collection model, String className, String path, Type containingType, IDictionary`2 inheritedMetas) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\XmlHbmBinding\CollectionBinder.cs:line 232
    at NHibernate.Cfg.XmlHbmBinding.CollectionBinder.CreateBag(HbmBag bagMapping, String prefix, String path, PersistentClass owner, Type containingType, IDictionary`2 inheritedMetas) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\XmlHbmBinding\CollectionBinder.cs:line 85
    at NHibernate.Cfg.XmlHbmBinding.CollectionBinder.Create(ICollectionPropertiesMapping collectionMapping, String className, String propertyFullPath, PersistentClass owner, Type containingType, IDictionary`2 inheritedMetas) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\XmlHbmBinding\CollectionBinder.cs:line 25
    at NHibernate.Cfg.XmlHbmBinding.PropertiesBinder.Bind(IEnumerable`1 properties, Table table, IDictionary`2 inheritedMetas, Action`1 modifier, Action`1 addToModelAction) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\XmlHbmBinding\PropertiesBinder.cs:line 100
    at NHibernate.Cfg.XmlHbmBinding.PropertiesBinder.Bind(IEnumerable`1 properties, IDictionary`2 inheritedMetas, Action`1 modifier) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\XmlHbmBinding\PropertiesBinder.cs:line 52
    at NHibernate.Cfg.XmlHbmBinding.PropertiesBinder.Bind(IEnumerable`1 properties, IDictionary`2 inheritedMetas) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\XmlHbmBinding\PropertiesBinder.cs:line 47
    at NHibernate.Cfg.XmlHbmBinding.RootClassBinder.Bind(HbmClass classSchema, IDictionary`2 inheritedMetas) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\XmlHbmBinding\RootClassBinder.cs:line 59
    at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddRootClasses(HbmClass rootClass, IDictionary`2 inheritedMetas) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\XmlHbmBinding\MappingRootBinder.cs:line 83
    at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddEntitiesMappings(HbmMapping mappingSchema, IDictionary`2 inheritedMetas) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\XmlHbmBinding\MappingRootBinder.cs:line 42
    at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.Bind(HbmMapping mappingSchema) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\XmlHbmBinding\MappingRootBinder.cs:line 29
    at NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:line 522

    The web.config:

      <configSections>
        <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
      </configSections>
    
        <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
            <session-factory name="NHibernate.Test">
                <property name="connection.driver_class">
                    NHibernate.Driver.SqlClientDriver
                </property>
                <property name="connection.connection_string">
                    Data Source=*****;Initial Catalog=Shop;Integrated Security=True
                </property>
                <property name="adonet.batch_size">10</property>
                <property name="show_sql">true</property>
                <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
                <property name="use_outer_join">true</property>
                <property name="command_timeout">60</property>
                <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
                <property name="proxyfactory.factory_class">
                    NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu
                </property>
            </session-factory>
        </hibernate-configuration>

    The mapping file:

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
        namespace="Agathas.Storefront.Model.Basket"
            assembly="Agathas.Storefront.Model">
    
        <class name="Basket" table="Baskets" lazy="false" >
    
            <id  name="Id" column="BasketId" type="guid">
                <generator class="guid" />
            </id>
    
            <bag name="Items" access="field.camelcase-underscore" inverse="true"
                 cascade="all-delete-orphan" lazy="true" >
                <key column="BasketId"/>
                <one-to-many class="BasketItem"></one-to-many>
            </bag>
    
            <many-to-one access="field.camelcase-underscore" name="DeliveryOption"
                         class="Agathas.Storefront.Model.Shipping.DeliveryOption"
                         column="DeliveryOptionId"
                         not-null="true"/>
        </class>
    
    </hibernate-mapping>

    and finally, the class itself:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Agathas.Storefront.Infrastructure.Domain;
    using Agathas.Storefront.Model.Products;
    using Agathas.Storefront.Model.Shipping;
    
    namespace Agathas.Storefront.Model.Basket
    {
        public class Basket : EntityBase<Guid>, IAggregateRoot
        {
            private IList<BasketItem> _items;
            private IDeliveryOption _deliveryOption;
    
            public Basket()
            {
                _items = new List<BasketItem>();
                _deliveryOption = new NullDeliveryOption();
            }
    
            public int NumberOfItems
            {
                get { return _items.Sum(i => i.Qty); }
            }
    
            public decimal BasketTotal
            {
                get { return ItemsTotal + DeliveryCost(); }
            }
    
            public decimal ItemsTotal
            {
                get { return _items.Sum(i => i.Qty * i.Product.Price); }
            }
    
            public void Add(Product product)
            {
                if (BasketContainsAnItemFor(product))
                    GetItemFor(product).IncreaseItemQtyBy(1);
                else
                    _items.Add(BasketItemFactory.CreateItemFor(product, this));
            }
    
            public BasketItem GetItemFor(Product product)
            {
                return _items.Where(i => i.Contains(product)).FirstOrDefault();
            }
    
            private bool BasketContainsAnItemFor(Product product)
            {
                return _items.Any(i => i.Contains(product));
            }
    
            public void Remove(Product product)
            {
                if (BasketContainsAnItemFor(product))
                {
                    _items.Remove(GetItemFor(product));
                }
            }
    
            public void ChangeQtyOfProduct(int qty, Product product)
            {
                if (BasketContainsAnItemFor(product))
                {
                    GetItemFor(product).ChangeItemQtyTo(qty);
                }
            }
    
            public int NumberOfItemsInBasket()
            {
                return _items.Sum(i => i.Qty);
            }
    
            public IEnumerable<BasketItem> Items()
            {
                return _items;
            }
    
            public decimal DeliveryCost()
            {
                return DeliveryOption.GetDeliveryChargeForBasketTotalOf(ItemsTotal);
            }
    
            public IDeliveryOption DeliveryOption
            {
                get { return _deliveryOption; }
            }
    
            public void SetDeliveryOption(IDeliveryOption deliveryOption)
            {
                _deliveryOption = deliveryOption;
            }
    
            protected override void Validate()
            {
                if (DeliveryOption == null)
                    base.AddBrokenRule(BasketBusinessRules.DeliveryOptionRequired);
    
                foreach (BasketItem item in this.Items())
                {
                    if (item.GetBrokenRules().Count() > 0)
                        base.AddBrokenRule(BasketBusinessRules.ItemInvalid);
                }
            }
        }
    
    }

    _items is a private field, which for some reason is causing the problem. Any ideas where I'm going wrong, here?

    Monday, June 13, 2011 11:37 AM

Answers

  • User-626088549 posted

    Seems they made some changes. No longer can you do:

            <bag name="Items" access="field.camlecase-underscore" inverse="true"
                 cascade="all-delete-orphan" lazy="true" >
                <key column="BasketId"/>
                <one-to-many class="BasketItem"></one-to-many>
            </bag>

    In order to access 'Items' with a friendly name instead of the private field: _items. Had to change it to this:

            <bag name="_items" access="field" inverse="true"
                 cascade="all-delete-orphan" lazy="true" >
                <key column="BasketId"/>
                <one-to-many class="BasketItem"></one-to-many>
            </bag>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 14, 2011 12:56 PM