nhibernate(或 hibernate)条件级联

发布于 2024-10-12 01:21:36 字数 173 浏览 7 评论 0原文

我有一个 User 类,它有一个属性 Event,它有很多会话。基本上,用户注册一个有很多会话时间的活动。

用户可以注册参加活动,但会议时间仅供参考。

但是当我使用 NH 将用户写入数据库时​​,它也会更新会话时间。我怎样才能防止这种情况发生,因为我知道在创建/更新活动时仍然需要插入/更新会话时间。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷了相思 2024-10-19 01:21:36

这可能并不完全适用于您的问题,但我遇到了一些问题,我想根据某些业务规则有条件地级联删除。

很多时候,您可以在持久性逻辑中处理这个问题。我遇到过一个使用 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(); 
                }
            }
        }
    }

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(); 
                }
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文