1

I have a class User, that has a property Event, which has many Sessions. Basically, a user register to an event which have many sessions hours.

The user can register for an event, but the sessions hours are purely informative.

But when I write a user to the database with NH, it updates the sessions hours too. How can I prevent that, knowing that I still need the sessions hours to be inserted/updated when I create/update an event.

Sebastien F.
  • 1,613
  • 2
  • 23
  • 40
  • A few questions: The User has an Event as a property? A user can only register for one event? Sessions - Is the user associated to the sessions, or only associated to the Event? – bzarah Jan 13 '11 at 17:49
  • It would also be very helpful to show us the code that maps your entities and the code you are calling to populate and save the User/Event/Sessions – bzarah Jan 13 '11 at 18:00
  • Actually I simplified the issue to the core of it because it's way more complex than that. So In that example, yes thevuser can only have one event and is only associated to the event not the sessions. – Sebastien F. Jan 13 '11 at 23:58
  • The user is simply mapped (using fluent nhibernate) and I'm just doing a References(u => u.Event). The sessions of the event are mapped as HasMany(e => e.Sessions) – Sebastien F. Jan 14 '11 at 00:03

1 Answers1

0

This might not totally apply to your question, but I've had issues where I wanted to conditionally cascade deletes based on certain business rules.

A lot of the times, you can handle this in your persistence logic. I had a case where I went with NHibernate Event Listeners.

public class ConditionalDeleter: IPostDeleteEventListener
    {
        public void OnPostDelete(PostDeleteEvent @event)
        {
            var foo = @event.Entity as Foo;
            if (foo != null)
            {
                if (foo.ShouldDeleteBar)
                {
                    ISession session = @event.Session.GetSession(EntityMode.Poco);
                    session.Delete(foo.Bar);

                    session.Flush(); 
                }
            }
        }
    }
Scott Coates
  • 2,462
  • 5
  • 31
  • 40