views:

728

answers:

3

I'm getting the error "Illegal acces to loading collection" when i'm trying to get a list of variants belonging to a certain product. The NHibernate mapping is as below;

<list name="Variants" lazy="false" cascade="save-update" inverse="false" table="PluginProduct_ProductVariant">
  <key column="ProductId" />
  <index column="Ordinal" />
  <one-to-many class="Plugin.Product.Business.Entities.Variant, Plugin.Product" />
</list>

I already tried chancing the laziness and inverse properties as suggested in other topics on this site, but they didn't do the trick.

I'm using NHibernate in combination with ASP.NET MVC and and i'm trying to loop trough a collection of variant in my view. The view is calling the following method;

        public ActionResult ShowProduct()
        {
        var id = new Guid(PluginData.PageParameters["Id"]);

        var variant = _variantService.GetVariantById(id);
        var product = variant.Product;

        return PluginView("ShowProduct.ascx", product);
        }

The above code runs without any problems. But when i debug just before returning the view i see that the list of variants which the product contains is empty. When i open more detailed debug information it's showing me the collection error. In the view off my webapplication i'm trying to do the following;

<%
foreach (var variant in Model.Variants)
{%>
    kleur: <%= variant.Color %>
    van: <%= variant.FromPrice %> voor: <%= variant.Price %>
<%} %>
A: 

Got the problem solved! I ran into an other problem adding a product with a variant so i changed this intelligence in my controller. Then i ran into a problem with the mapping so i changes the mapping as below and it all worked!

    <list name="Variants" lazy="false" cascade="all" inverse="false">
  <key column="ProductId" />
  <index column="Ordinal" />
  <one-to-many class="Plugin.Product.Business.Entities.Variant, Plugin.Product" />
</list>
Rob
A: 

Ok apperently the problem wasn't fixed. I got it working with one variant for each product, but now i made modifications so i can add more variants to one product. and i'm getting the same error in my view again.

The view is as below <% foreach (var variant in Model.Variants) {%> kleur: <%= variant.Color %>
<%} %>

And my mapping is like this

<joined-subclass name="Plugin.Product.Business.Entities.Product, Plugin.Product" extends="CMS.Business.Entities.BaseEntity, CMS.Business.Entities" table="PluginProduct_Product">
<key column="Id" />
<property name="Name" />
<property name="Description" />
<property name="ShippingCost" />
<property name="Tax" />
<property name="Brand" />
<list name="Categories" table="PluginProduct_ProductHasCategory" cascade="save-update" inverse="false">
  <key column="ProductId" />
  <index column="Ordinal" />
  <many-to-many class="Plugin.Product.Business.Entities.Category, Plugin.Product" column="ProductCategoryId" />
</list>
<list name="Variants" table="PluginProduct_ProductVariant" lazy="true" cascade="all" inverse="false">
  <key column="ProductId" />
  <index column="Ordinal" />
  <one-to-many class="Plugin.Product.Business.Entities.Variant, Plugin.Product" />
</list>

Rob
Could someone please help me out? The problem is getting a little urgent.
Rob
+2  A: 

Ok very stupid but i finally got the problem solved. The index column Ordinal in the database wasn't getting the correct values so it was always NULL. This caused the error because NHibernate couldn't find an index column to create the list on. Caused me a lot of time unfortunately, but glad i got it solved!

Rob

related questions