天天看点

spring4.x不再提供 HibernateTemplate.saveOrUpdateAll(Collection<T>) 的支持

    最近要把项目从spring 3.x 转到 4.x,虽然很多原来的代码都可使用,但如果新版本能兼容你的老项目,那官方的更新就没意义了(逃。。。

报错:

    The method saveOrUpdateAll(Collection<T>) is undefined for the type HibernateTemplate

原因:如果你观看spring2<API FOR SPRING2> 和spring 3的文档 <API FOR SPRING3>。你会发现:从 spring2.5 起,官方就建议不要使用该方法,并且在 spring4.x中完全摈弃了这个方法。

saveOrUpdateAll 的声明如下:

public void saveOrUpdateAll(Collection entities) throws DataAccessException      

参数中接收一个Collection。

原项目中代码如此:

super.getHibernateTemplate().saveOrUpdateAll(entitys);
           

可以修改为这样:

for(T t : entities) {
   getHibernateTemplate().saveOrUpdate(t);
}