tags:

views:

78

answers:

3

when i use saveOrUpdate(Object) method of Hibernate. How can I know that row 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.

+1  A: 

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

daedlus
yes you are right daedlus, but like when we use save() method it return serializable which we can store in Long variable and if it is greater than 0L means row added but when we use saveOrUpdate() method it doesn't return anything so how can we know its is updated or added any row. Any method or code??Thanks in Advance
Vikram
is the above check not sufficient to know if row was inserted or updated? what is your use case?
daedlus
+1  A: 

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.

isuruceanu
A: 

Thanks isuruceanu, update() method also return void, then how can we now row is updated?

Vikram