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.  [ 2 posts ] 
Author Message
 Post subject: NHibernate with ReadOnlyCollections
PostPosted: Fri Oct 02, 2009 1:42 pm 
Newbie

Joined: Fri Oct 02, 2009 1:12 pm
Posts: 3
Hey all, new NHibernate user with a problem I hope you can solve.

Let's say I had a class that implemented a recursive list of scheduled tasks. A "task" can be a single "process", or a child "schedule" of multiple tasks. A simplification of Schedule would be as follows:

Code:
public class Schedule:Task
{
   private IList tasks = new IList<Task>

   public AddNewTask(Task task)
   {
      //do some validation and add task to tasks
   }

   public DeleteTask(Task task)
   {
      //do some validation and remove the task from the list
   }

   public ReadOnlyCollection<Task> Tasks {get{return new ReadOnlyCollection<Task>(tasks);}}
}


The intent, of course, is to prevent manipulation of the list except through the class methods. This model predates my involvement with the codebase, and is not easily refactored to implement a pure method- or property-based pattern.

The relevant NHibernate elements are below:

Code:
<list name="tasks" table="ScheduleTask" access="field.lowercase" lazy="false" cascade="save-update" batch-size="10">
      <key column="ScheduleID"
           foreign-key="FK_Task_ScheduleID"/>
      <index column="TaskIndex"/>
      <many-to-many class="MyProject.Task, MyProject.Domain" column="TaskID"/>
    </list>


The problem I am having is when I persist a new schedule, NHibernate tries to use the Tasks property to replace its proxy classes with real data. This occurs even though the access is by field and we are forcing it to use the name as specified.

Everything I've read in the docs say that this should behave as I intend; NHibernate SHOULD be reflectively accessing the backing field directly when hydrating, while all my code objects are forced to abide by the class' stricter access rules. Even the integrated tests are working properly, but production code still fails. Help!


Top
 Profile  
 
 Post subject: Re: NHibernate with ReadOnlyCollections
PostPosted: Sun Oct 04, 2009 11:35 am 
Newbie

Joined: Sun Oct 04, 2009 5:36 am
Posts: 8
Hi, try this:

Code:
public class Schedule:Task
{
   private IList tasks = new IList<Task>

   public AddNewTask(Task task)
   {
      //do some validation and add task to tasks
   }

   public DeleteTask(Task task)
   {
      //do some validation and remove the task from the list
   }
   public virtual IList<Task> Tasks
        {
            get
            {
                return _tasks.ToList<Task>().AsReadOnly();
            }
        }

}


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

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.