-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Bidirectional IdBag Collection modified exception
PostPosted: Sun Mar 30, 2008 6:14 pm 
Newbie

Joined: Sun Mar 30, 2008 5:19 pm
Posts: 8
I have this situation: (simplified for ease of reading)
2 Business entities with bi-directional many-to-many relationship.
Code:
namespace TestProject
{
   public class Program
   {
      private Decimal _programId;
      private String _name;
      private IList<Project> _projects = new List<Project>();
      public virtual Decimal ProgramId { get; set; }
      public virtual String Name { get; set; }
      public virtual IList<Project> Projects
      {
         get
         {
            return new List<Project>(_projects).AsReadOnly();
         }

         protected set
         {
            _projects = value;
         }
      }
   }
   public class Project
   {
      private Decimal _projectId;
      private String _name;
      private IList<Program> _program = new List<Program>();
      public virtual Decimal ProjectId { get; set; }
      public virtual String Name { get; set; }
      public virtual IList<Program> Programs
      {
         get
         {
            return new List<Program>(_programs).AsReadOnly();
         }

         protected set
         {
            _programs = value;
         }
      }
   }
}


with these mappings:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Program" table="PROGRAM">
    <id name="ProgramId" type="Decimal">
      <column name="PROGRAM_ID"/>
      <generator class="sequence">
        <param name="sequence">PROGRAM_SQ</param>
      </generator>
    </id>
    <property name="Name" type="String">
      <column name="NAME" length="50" not-null="true"/>
    </property>
    <idbag name="Projects" inverse="true" table="PROGRAM_PROJECT">
      <collection-id column="PROGRAM_PROJECT_ID" type="Decimal">
        <generator class="sequence">
          <param name="sequence">PROGRAM_PROJECT_SQ</param>
        </generator>
      </collection-id>
      <key>
        <column name="PROGRAM_ID" not-null="true"/>
      </key>
      <many-to-many class="Project" column="PROJECT_ID"/>
    </idbag>
  </class>
  <class name="Project" table="PROJECT">
    <id name="ProjectId" type="Decimal">
      <column name="PROJECT_ID"/>
      <generator class="sequence">
        <param name="sequence">PROJECT_SQ</param>
      </generator>
    </id>
    <property name="Name" type="String">
      <column name="NAME" length="100" not-null="true"/>
    </property>
    <idbag name="Programs" inverse="true" table="PROGRAM_PROJECT">
      <collection-id column="PROGRAM_PROJECT_ID" type="Decimal">
        <generator class="sequence">
          <param name="sequence">PROGRAM_PROJECT_SQ</param>
        </generator>
      </collection-id>
      <key>
        <column name="PROJECT_ID" not-null="true"/>
      </key>
      <many-to-many class="Program" column="PROGRAM_ID"/>
    </idbag>
  </class>
</hibernate-mapping>



If you notice, I have set inverse="true" on both sides of the relationship for now - I will make one side responsible after I figure out this issue.

I have populated the database with a few Programs, a few Projects, and populated the intermediary PROGRAM_PROJECT table so they have ties back to each other.

NOTE: This relationship is LAZY at both ends also. (I have tried putting lazy="true" in each idbad and still get the same exception)

For this example lets say I have some Projects named Test 1, Test 2, Test 3, Testing 1, Testing 2, Testing 3.
Code:
ICriteria criteria = session.CreateCriteria(typeof(Project)).Add(Expression.Like("Name", "%Testing%"));
IList<Project> Projects = criteria.List<Project>() as List<Project>;

This gives me Count = 3 and works fine.
The problem begins when I execute the next segment of code to get a list of all Programs (to populate a dropdown to use as a filter for listing projects by program).
Note I use criteria here as I will filter out inactive items using a status field later on, but for now i'll leave that out.
Code:
ICriteria program_criteria = session.CreateCriteria(typeof(Program));
IList<Program> Programs = program_criteria.List<Program>() as List<Program>;


This throws the following exception:
Code:
Server Error in '/' Application.
--------------------------------------------------------------------------------

Collection was modified; enumeration operation may not execute.


I can't for the life of me figure out why this would happen. Any help would be GREATLY appreciated.

Thanks in advance.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.