简体   繁体   English

为什么在Spring HibernateOperations中不推荐使用saveOrUpdateAll

[英]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) 这背后可能有正当理由,但奇怪的是我可以做hibernate.deleteAllhibernate.loadAll ,但不是hibernate.saveOrUpdateAll (确定我可以做到这一点,但是如果这个方法被弃用意味着它可能会在下一个版本中消失)

The reason is clear from the Java docs, 原因很明显来自Java文档,

in favor of individual saveOrUpdate or merge usage 支持单个saveOrUpdate或合并用法

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. 因此,spring希望您迭代列表并保存单个对象。

The loadAll() method is different and useful. loadAll()方法不同且有用。 It is not similar to saveOrUpdateAll() . 它与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. 你的观察结果是正确的, deleteAll()类似于saveOrUpdateAll() ,我同意它是不一致的,一个是不赞成的而另一个不是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM