-->
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: Does NHibernate need parameterized constructor support?
PostPosted: Wed Oct 05, 2005 3:33 pm 
Beginner
Beginner

Joined: Wed Oct 05, 2005 2:08 pm
Posts: 23
I have a 1 to many relationship between a parent and child class. All of the child instances in a given parent class must have a unique value in the "Number" property. The "Number" property is not globally unique, it must only be unique within the parent.

The parent has a lazily loaded collection of child objects. Each child object has a Parent property. I would like to throw an exception if a child with a "Number" property equal to 3 (for example) is added to a Parent that already has a Child with a "Number" property equal to 3.
Code:
Parent parent = ... // retrieve from NHibernate
Child child = new Child();
child.Number = 3;
child.Parent = parent; // throws Exception if # already exists in any Child in parent except for this child instance

parent.Children.Add(child);



The Child class is defined as follows:

Code:
// defines property that exposes the new number value, allows action to be cancelled by parent
class NumberChangedCancelEventArgs : System.ComponentModel.CancelEventArgs
{
//...


}

delegate void NumberChangedHandler(object source, NumberChangedCancelEventArgs args);

class Child {

   public event NumberChangedHandler NumberChanged ;

   private Parent _parent = null;
   public Parent Parent{

      get{return _parent;}
      set{
         if (_parent != null)
            NumberChanged -= new NumberChangedHandler (_parent.OnChildNumberChanged);

         _parent = value;

         // add parent as listener for NumberChanged event
         NumberChanged += new NumberChangedHandler (_parent.OnChildNumberChanged);
   
      }
   }

   public int Number {
      get{return _number;}
      set{
         if (_number != value){
            NumberChangedCancelEventArgs args = new NumberChangedCancelEventArgs (value);
            NumberChanged(this,args);

            // if Parent cancels because it already contains a Child with the new number
            if (args.Cancel)
               throw new Exception("The Parent already contains a Child with that number.");

            _number = value;
         }
      }
   }

}


My problem is that I can't rely on NHibernate to initialize the properties in any particular order. As a result if
NHibernate initializes the Number property before the Parent property, the Parent will not be informed of the NumberChanged event. Even if NHibernate initializes the properties in the correct order when lazily loading the Child collection I don't want to alert the Parent that the Number has changed the first time it is set. After all, if a Child is loaded from the database it's number property must be unique and double-checking it when the property is first set wastes valuable cycles,.


What I need is for NHibernate to initialize my Child object using a parameterized constructor so that I know if a property set is called it is not being initialized but actually being changed. I can also do event wireups at the end of the parameterized constructor.

Is there a way of using parameterized constructors or perhaps a better approach entirely?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 3:23 am 
Regular
Regular

Joined: Fri Jun 11, 2004 6:27 am
Posts: 81
Location: Yaroslavl, Russia
Actually, your post shoul be titled "How to avoid using property accessors by NHibernate?". :)

In you mapping class, at the "Number" property mapping specify access="field.camelcase-underscore". This instructs NH to write and read the property value using the fields instead of accessor methods.

_________________
Best,
Andrew Mayorov // BYTE-force


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.