A couple of things stick out to me on first glance. Mainly that you are mapping to a method, why not a property? I guess it is possible, it just isn't standard practice. I've never done it, I assume it works though based on your mapping I don't think fluent will know how to map the collection back to parent (i.e., I don't think it will assume it should set _childs).
This actually may be your problem. You are getting a Child, which in turn accesses the Parent (lazy loaded) to update total, the Parent must in turn access it's Children (again through lazy loading). That may be where your problem is, I don't think you are getting those children. If you have show_sql on you should see the query for the children. Also, I don't typically use ISet<T>, not that there is anything wrong with it, it's just that I see it as a hangover from Java that nHibernate supports just to be as compliant as they can be. I rarely see code or even tutorials using anything other than .NET IList<T>. You shouldn't have any problems switching, not that I am suggesting this is your problem, you just might want to try it. On Tue, Jan 19, 2010 at 3:34 PM, Eric Anderson <[email protected]>wrote: > I cannot seem to change the value of a mapped property on an entity. > I have a simple parent / child relationship mapped fluently as > follows: > > public class ParentMap : ClassMap<Parent> > { > public ParentMap() > { > Cache.ReadWrite(); > DynamicUpdate(); > Id(x => x.Id).GeneratedBy.GuidComb(); > > HasMany(x => x.GetChilds()) > .AsSet() > .Inverse() > .Cascade.AllDeleteOrphan(); > > Map(x => x.Total); > } > } > > public class ChildMap : ClassMap<Child> > { > public ChildMap() > { > Cache.ReadWrite(); > DynamicUpdate(); > Id(x => x.Id).GeneratedBy.GuidComb(); > > References(x => x.Parent); > > Map(x => x.Total); > } > } > > ----------------------------------------------------------- > The classes themselves look like: > > public class Parent : PersistentObject > { > private readonly ISet<Child> _childs = new > HashedSet<Child>(); > > public virtual int Total { get; private set; } > > protected internal virtual void UpdateTotal() > { > var newTotal = GetChilds().Sum(x => x.Total); > Total = newTotal; > } > > public virtual Child AddChild(Child child) > { > _childs.Add(child); > child.Parent = this; > return child; > } > > public virtual Child[] GetChilds() > { > return _childs.ToArray(); > } > } > > > public class Child : PersistentObject > { > public virtual Parent Parent { get; set; } > public virtual int Total { get; private set; } > > public virtual void ResetTotal(int newTotal) > { > Total = newTotal; > Parent.UpdateTotal(); > } > } > ----------------------------------------------------------- > > The problem is, when I have loaded a Child from the database, and I > call ResetTotal() on the child, this should bubble up to calling > Parent.ResetChild(). In the course of that call on the Parent, I can > step through and see that Parent.Total is hitting the assignment > statement (Total = newTotal;), but this doesn't actually change the > value that is stored in Total. > > Here are two test cases -- one that fails because it uses a persisted > and reloaded instance of a Child, and one that works because it uses > non-persistent instances: > > [Test] > public void > Should_update_total_for_parent___fails_when_parent_is_proxied() > { > var parent = new Parent(); > var child = parent.AddChild(new Child()); > PersistEntities(parent); > > using (var session = GetSession()) > { > var loaded = session.Load<Child>(child.Id); > loaded.ResetTotal(3); > Parent referencedParent = loaded.Parent; > referencedParent.Total.ShouldEqual(3); > } > } > > [Test] > public void > Should_update_total_for_parent___works_fine_for_non_persistent_entities > () > { > var parent = new Parent(); > var child = parent.AddChild(new Child()); > > child.ResetTotal(3); > Parent referencedParent = child.Parent; > referencedParent.Total.ShouldEqual(3); > } > ----------------------------------------------------------- > > Is there some cache setting that I need to change? It just seems like > the ParentProxy is getting in the way and not properly wired up to the > underlying field on the auto property. > > -- > 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]<nhusers%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/nhusers?hl=en. > > > >--
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.
