简体   繁体   English

nhibernate(或休眠)条件级联

[英]nhibernate (or hibernate) Conditional cascading

I have a class User, that has a property Event, which has many Sessions. 我有一个User类,它具有一个Event事件属性,其中包含许多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. 但是,当我用NH将用户写入数据库时​​,它也会更新会话时间。 How can I prevent that, knowing that I still need the sessions hours to be inserted/updated when I create/update an event. 我知道在创建/更新事件时仍需要插入/更新会话时间,因此如何避免这种情况。

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. 我曾经遇到过NHibernate事件监听器的情况。

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(); 
                }
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM