illegal access to loading collection error
Asked Answered
P

5

18

I'm getting the error

Illegal access 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 through 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 of my web application I'm trying to do the following

<%
foreach (var variant in Model.Variants)
{%>
    kleur: <%= variant.Color %>
    van: <%= variant.FromPrice %> voor: <%= variant.Price %>
<%} %>
Phototube answered 22/3, 2010 at 15:19 Comment(0)
P
10

Okay, 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.

Cost me a lot of time unfortunately, but glad I got it solved!

Phototube answered 29/3, 2010 at 15:13 Comment(0)
P
2

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>
Phototube answered 23/3, 2010 at 16:15 Comment(0)
G
2

inverse="true" is the most commonly used, because it means that the other endpoint is the one that has the key in one to many associations (the many side has a foreign key to the one side).

Garboard answered 9/8, 2011 at 11:17 Comment(0)
I
1

I got this problem and it wasn't a mapping problem but actually a data problem. We received way too much data in our collections, but we got this exception anyway instead of something more useful.

I expected my collection to contain 10-15 records but it had some 4 million records.

Innovation answered 7/2, 2014 at 13:53 Comment(1)
it could also be a case of a cross product (see tutorialspoint.com/sql/sql-cartesian-joins.htm) which you may or may not have wanted.Cervin
S
0

The many to one property isn´t mapped corretly in the other map class, so it will not bring results to relate it to.

Basically I deleted the line: map.PropertyRef("Codigo"); And it has worked normally.

 ManyToOne(x => x.Menu, map => 
            {
                    map.Column("COD_MENU");
                    //map.PropertyRef("Codigo");
                    map.NotNullable(true);
                    map.Cascade(Cascade.None);
            });
Swathe answered 14/7, 2014 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.