开发者

saveOrUpdate(Object) method of Hibernate

when i use saveOrUpdate(Object) method of Hibernate. How can I know that row开发者_如何学C is updated or new row added into table??? Return type of method saveOrUpdate(Object) is void, so am not able to find out the result after calling this method.

kindly help me.


As I understand from your question and comment lived. You could create an event listener and implement two Interfaces: IPreUpdateEventListener, IPreInsertEventListener

e.q.

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

but I think this is ridiculous. using ORMs means that you do not care about persistence and all work is done the unitofwork. If you really need to insert and update just use Save() or Update() methods, in this way you will exactly know what operation is made.


If your object to be persisted is having identifier property set to 0(/id null) then it means it is a new object and will be inserted newly in db. After it is inserted hibernate will then set the id value in identifier field. If already the object has identifier property set then it means the object is already persisted and it can be updated

EDIT:Did you look at hibernate interceptors?May be this is useful. example

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜