Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why saveOrUpdateAll is deprecated in spring HibernateOperations

Take a look into source code.

There could be valid reasons behind that, but it's strange that I could do hibernate.deleteAll and hibernate.loadAll, but not hibernate.saveOrUpdateAll (sure I can do that practically, but if this method is deprecated means that it could disappear in next release)

like image 315
Petro Semeniuk Avatar asked Apr 13 '12 07:04

Petro Semeniuk


People also ask

Is HibernateTemplate deprecated?

The remaining operations on this HibernateTemplate are deprecated in the meantime and primarily exist as a migration helper for older Hibernate 3.

What is difference between save and saveOrUpdate in hibernate?

save() generates a new identifier and INSERT record into the database while saveOrUpdate can either INSERT or UPDATE based upon the existence of a record. Clearly, saveOrUpdate is more flexible in terms of use but it involves extra processing to find out whether a record already exists in the table or not.

What is the use of HibernateTemplate in spring?

Spring's HibernateTemplate provides an abstract layer over a Hibernate Session. It converts Hibernate-specific exceptions to one of the Spring's unchecked data-access exception. It also provides many convenience methods that help you in querying and persisting objects.

How hibernate saveOrUpdate works?

When you use . saveOrUpdate() Hibernate will check if the object is transient (it has no identifier property) and if so it will make it persistent by generating it the identifier and assigning it to session. If the object has an identifier already it will perform . update() .


1 Answers

The reason is clear from the Java docs,

in favor of individual saveOrUpdate or merge usage

As you can see the implementation of the method, code taken from the link,

public void saveOrUpdateAll(final Collection entities) throws DataAccessException {
    executeWithNativeSession(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException {
            checkWriteOperationAllowed(session);
            for (Iterator it = entities.iterator(); it.hasNext(); ) {
                session.saveOrUpdate(it.next());
            }
            return null;
        }
    });
}

This method was less used, it can not be inside your transaction. So, spring want you to iterate the list and save the individual objects.

The loadAll() method is different and useful. It is not similar to saveOrUpdateAll().

You are right with your observations that deleteAll() is similar to saveOrUpdateAll() and I agree that it is inconsistent one is deprecated and the other one is not.

like image 147
ManuPK Avatar answered Sep 25 '22 10:09

ManuPK