Hibernate的saveOrUpdate(Object)方法

时间:2010-06-14 10:35:35

标签: nhibernate

当我使用Hibernate的saveOrUpdate(Object)方法时。我怎么知道行更新或新行添加到表???方法的返回类型saveOrUpdate(Object)为void,因此在调用此方法后无法找到结果。

请帮助我。

2 个答案:

答案 0 :(得分:1)

如果要保留的对象将identifier属性设置为0(/ id null),则表示它是一个新对象,并将在db中新插入。 插入后,hibernate将在标识符字段中设置id值。 如果对象已经设置了标识符属性,则表示该对象已被持久化并且可以更新

编辑:你看过hibernate拦截器吗?可能这很有用。 example

答案 1 :(得分:1)

据我所知,你的问题和评论一直存在。您可以创建一个事件侦听器并实现两个接口:IPreUpdateEventListener,IPreInsertEventListener

如参见

     public class AuditEventListener : IPreUpdateEventListener, IPreInsertEventListener
    {

        public bool OnPreUpdate(PreUpdateEvent @event)
        {
            //your stuff here
            return false;
        }

        public bool OnPreInsert(PreInsertEvent @event)
        {

            //your stuff here
            return false;
        }
}

但我认为这很荒谬。使用ORM意味着您不关心持久性,所有工作都是通过单元完成的。如果你真的需要插入和更新只使用Save()或Update()方法,这样你就可以准确地知道做了什么操作。

相关问题