Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 401799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:04:01+00:00 2026-05-12T17:04:01+00:00

Im just starting to work with NHibernate. I’ve setup a simple many-to-many scenario using

  • 0

Im just starting to work with NHibernate. I’ve setup a simple many-to-many scenario using Products and Suppliers as follows:

<class name="Product" table="Products">
    <id name="Id">
      <generator class="guid" />
    </id>
    <property name="Name" />

    <bag name="SuppliedBy" table="ProductSuppliers" lazy="true">
      <key column="ProductId" foreign-key="FK_ProductSuppliers_ProductId" />
      <many-to-many column="SupplierId" class="Supplier" />
    </bag>
</class>

<class name="Supplier" table="Suppliers">
    <id name="Id">
      <generator class="guid" />
    </id>
    <property name="Name" />

    <bag name="Products" table="ProductSuppliers" lazy="true" inverse="true">
      <key column="SupplierId"  foreign-key="FK_ProductSuppliers_SupplierId" />
      <many-to-many column="ProductId" class="Product" />
    </bag>
</class>

I’m now trying to wire the bags up to my domain objects. From reading the documentation I came up with (Using Iesi.Collections lib):

'In Product
Private _Suppliers As ISet = New HashedSet()
Public Overridable Property SuppliedBy() As HashedSet
    Get
        Return _Suppliers
    End Get
    Set(ByVal value As HashedSet)
        _Suppliers = value
    End Set
End Property

'In Supplier
Private _Products As ISet = New HashedSet()
Public Overridable Property Products() As HashedSet
    Get
        Return _Products
    End Get
    Set(ByVal value As HashedSet)
        _Products = value
    End Set
End Property

However, when I try and add a supplier to a product and call save I get the following error

Unable to cast object of type ‘NHibernate.Collection.PersistentBag’ to type ‘Iesi.Collections.HashedSet

I’ve tried using all kinds of types, for example, ICollection and List(Of T) but I keep getting the equivalent error.

Unable to cast object of type ‘NHibernate.Collection.Generic.PersistentGenericBag1[Domain.Supplier]' to type 'System.Collections.Generic.List1[Domain.Supplier]

What am I missing here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:04:01+00:00Added an answer on May 12, 2026 at 5:04 pm

    The documentation talks about creating a bag using an IList, or IList(of Entity) and constructing it with the List, or List(of Entity). (Section 6.2 of the NHibernate 1.2 Reference).

    The Semantics of a bag do not match that of a Set which is that a Set can only have unique instances and a bag can have duplicate instances. As a fair comment a List does not match exactly the semantics of a bag either (a bag does not have an index) but it’s close enough for NHibernate.

    Your collection mapping should then be (using generics – take (of Supplier) out to remove generics:

    'In Product
    Private _Suppliers As IList(of Supplier) = New List(of Supplier)
    Public Overridable Property SuppliedBy() As IList(of Supplier)
        Get
            Return _Suppliers
        End Get
        Set(ByVal value As IList(of Supplier))
            _Suppliers = value
        End Set
    End Property
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just starting looking at a site in place at work. They're using Elmah
I'm just starting to work with JCR (apache jackrabbit), i want to ask simple
I'm working on a project and I'm just starting to do all the work
Just starting to learn the CakePHP framework. I'm using the Auth component, and all
Just starting to learn ASP.NET (C#) and I am using Visual Studio 2008. I
Just starting out, this should be a simple one but I haven't been able
I'm just starting to work with JSF and Facelets and I've put together a
I'm just starting to work on a logging library that everyone can use to
I'm just starting to work with Silverlight (no WPF experience either) and am having
I am just now starting to work with LINQ, and am pretty familiar with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.