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)
The remaining operations on this HibernateTemplate are deprecated in the meantime and primarily exist as a migration helper for older Hibernate 3.
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.
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.
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() .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With