0
votes

I have a parent class which contains a child object. I am using set to save the child object when parent is saved. I m not sure whether set is used for just saving a child object. And im getting this error below

System.InvalidCastException: Unable to cast object of type 'x' to type 'Iesi.Collections.ISet'

Does anybody knows the solution.

<class name="Customer" table="Customers" lazy="false" dynamic-update="true">

  <id name="Id" column="CustomerID" type="Guid" >
  <generator class="guid.comb" />
  </id>

  <property name="Name" column="CustomerName" type="String" length="50" not-null="true" />

 <bag  name="users" table="Users" cascade="all-delete-orphan">
  <key column="CustomerID" />
  <one-to-many class="User" />
</bag>

<set name="customerPreferences" table="Preferences" cascade="all-delete-orphan">
  <key column="CustomerID" />
  <one-to-many class="CustomerPreferences" />
</set>

</class>

Entity class: public class Customer { private Guid _id; private string _name; private IList _users = new List(); private CustomerPreferences _customerPreferences;

    public Guid Id
    {
        get { return _id; }
        set { _id = value; }
    }

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public CustomerPreferences CustomerPreferences
    {
        get { return _customerPreferences; }
        set { _customerPreferences = value; }
    }

    public IList<User> Users
    {
        get { return _users; }
        set { _users = value; }
    }
1
Can you include your mapping file and entity?Nigel
Yeah i have added the entity and im getting error in customerpreferences not usersalice7

1 Answers

0
votes

Your mapping files look fine. So I am guessing that the problem is to do with your implementation of the CustomerPreferences class.

The set attribute in the mapping file defines a property that (when mapped to your entity) must implement the Iesi.Collections.Generic.ISet<T> interface. Apparently you can also use the standard ICollection<T> interface with NHibernate 2.1 but I have never tried this.

Therefore if your CustomerPreferences class does not implement the ISet<T> interface then you will get an InvalidCastException.