我正在尝试升级应用程序以使用最新版本的 Spring。目前使用 Spring 3.0.7 和 Hibernate 3.5.6。我能够成功地将其升级到 Spring 3.2.8,但是如果我将其升级到 Spring 4,则会出现如下编译错误:
[ERROR] /Users/mraible/dev/foo-upgrades/foo-core/src/main/java/com/company/foo/dao/companydb/impl/hibernate/InFaxDAOImpl.java:[71,82] incompatible types
[ERROR] required: java.util.List<com.company.foo.common.domain.FaxPage>
[ERROR] found: java.util.List<capture#49 of ?>
方法调用如下所示:
List<FaxPage> faxPages = getHibernateTemplate().findByNamedQueryAndNamedParam(queryName, hqlParams, values);
我可以通过转换为列表类型来修复编译错误,但这似乎不对。
List<FaxPage> faxPages = (List<FaxPage>) getHibernateTemplate().findByNamedQueryAndNamedParam(queryName, hqlParams, values);
Spring 4 是什么破坏了 HibernateDaoSupport,因此它不向后兼容?
最佳答案
这是因为 Spring 框架过去常常返回原始列表声明,而该原始列表声明基本上允许您声明您想要的任何类型。如果这样做,编译器将发出警告,可以使用 @SuppressWarnings("unchecked")
声明“忽略”该警告。
虽然可以为此提供向后兼容性,但许多引用文献建议永远不要在“新代码”中使用原始列表。
引用the Java Language Specification
The use of raw types is allowed only as a concession to compatibility of legacy code. The use of raw types in code written after the introduction of genericity into the Java programming language is strongly discouraged. It is possible that future versions of the Java programming language will disallow the use of raw types.
This thread是对此事的一个很好的总结。
虽然我当然可以理解您以前能够执行的操作的好处,但我建议您像以前一样更新代码,是的:无论如何,您之前都在进行隐式转换。
关于java - 从 Spring 3 升级到 Spring 4 后,HibernateTemplate 出现不兼容类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23210530/