Yes, there is a way. This is how I do it:
definition:
protected virtual int NumAddresses { get { return Addresses.Count; }}
mapping:
<property name="NumAddresses" *access="readonly"*/>
This way, the field is kept in sync with the number of addresses in the
collection.
One important thing to consider is that the Addresses will always be loaded,
when the value of NumAddresses is read to check if User is dirty, so it's a
good idea to make the collection non-lazy by default.
Diego
On Thu, Nov 12, 2009 at 05:27, Kevin Pang <[email protected]> wrote:
>
> If I have a Users table and an Addresses table with a one-to-many
> relation between them (one user can have many addresses), what's the
> best way to handle mapping a "NumAddresses" column to the Users table
> for denormalization purposes?
>
> Right now my code is basically handling this manually:
>
> public virtual void AddAddress(Address address)
> {
> this.Addresses.Add(address);
> this.NumAddresses++;
> }
>
> However, I was wondering if NHibernate had some sort of built-in
> mechanism for handling this.
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---