I believe this is basically answered on stackoverflow...
http://stackoverflow.com/questions/25279478/nhibernate-does-not-work-with-tree-object/
On Thursday, 14 August 2014 10:09:52 UTC+2, Guybrush Threepwood wrote:
>
> hi, i have a question about how to get tree object working with NHibernate.
> i don't want to have parent or parentId property on my object since i have
> no need of it in my business layer and so this is my object:
>
> public class Tag
> {
> public int Id { get; set; }
> public int Description { get; set; }
> private IList<Tag> childTag = new List<Tag>();
> public virtual IEnumerable<Tag> ChildTag
> { get { return childTag.ToArray(); } }
>
> public virtual void Add(Tag child)
> { childTag .Add(child); }
>
> public virtual bool Remove(Tag child)
> { return childTag .Remove(child); }
> }
> with this map:
>
> public TagMap()
> {
> Id(x => x.Id)
> .Column("Kint_T01_IdTag")
> .GeneratedBy.Assigned();
> HasMany(x => x.ChildTag)
> .KeyColumn("ParentId")
> .Cascade.All()
> .Access.CamelCaseField().ReadOnly();
> }
>
> (i try also adding .Not.Inverse() but nothing change)
>
> when i run this test
>
> Tag Tag= fixture.Build<Tag>().Do(x => x.Add(fixture.Create<Tag>())).
> Create();
> Tag TagActual;
>
> using (IUnitOfWork uow = new UnitOfWork())
> {
> uow.openConnection();
> uow.TagRepository.Create(Tag);
> uow.commit();
> Tag.Rmove(Tag.ChildTag.First());
> uow.TagRepository.Update(Tag);
> uow.commit();
> uow.closeConnection();
>
> uow.openConnection();
> TagActual = uow.TagRepository.GetById(Tag.Id);
> TagActual.ShouldBeEquivalentTo(Tag);
> uow.closeConnection();
> }
>
> it fails because TagActual.ChildTag contains a child even if i remove it
> from parent collection.
>
> debugging the test i see that after
>
> uow.TagRepository.Create(Tag);
> uow.commit();
>
> two record are inserted in table and `parent` property of children is set
> correctly
>
> `Tag.Rmove(Tag.ChildTag.First()); ` work correctly and after that the
> collection is empty
>
> but after
>
> uow.TagRepository.Update(Tag);
> uow.commit();
>
> nothing change in table and 'parent' filed is still set.
>
> In this way the test failed because TagActual has a collection of children
> while this should be empty
>
> Why NHibernate correctly manage the `add` operation but not the `remove`
>
> Is there a way to do it without adding `Parent` property to my object and
> manually manage it?
>
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.