Maven 中的许多依赖项也依赖于其他库来运行。当包含像 hibernate-core 这样的依赖项时,它还会自动将其依赖项导入您的项目,其中之一是 hibernate-annotations 库。结果是 2 个具有不同版本的库,这是 Maven 中的一个常见问题,它们提供了一种很好的方法来查找这些依赖项是从哪里导入的。
在这种情况下,只需从您的项目中删除 hibernate-annotations 依赖项,然后让 hibernate-core 为您导入它。
如何自己找到这个重复的库冲突? 一个很好的方法是使用 Maven 提供给我们的依赖树插件:mvn dependency:tree -Dverbose -Dincludes=org.hibernate.*。这将显示项目中所有在 org.hibernate 的组或子组中的库依赖项。 还可以考虑通过在调用中附加> fileOutputName.txt 来输出到文件,以便更容易查看,尤其是在您使用 Windows 命令行时。
这是我拥有的具有休眠功能的项目的示例输出:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.0.1.Final</version>
</dependency>
还有mvn dependency:tree 电话:
[INFO] +- org.hibernate:hibernate-core:jar:4.0.1.Final:compile
[INFO] | +- (org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.1.Final:compile - omitted for duplicate)
[INFO] | \- org.hibernate.common:hibernate-commons-annotations:jar:4.0.1.Final:compile
[INFO] +- org.hibernate.common:hibernate-commons-annotations:jar:tests:4.0.1.Final:compile
[INFO] +- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.1.Final:compile
[INFO] \- org.hibernate:hibernate-entitymanager:jar:4.0.1.Final:compile
[INFO] +- (org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.1.Final:compile - omitted for duplicate)
[INFO] \- (org.hibernate.common:hibernate-commons-annotations:jar:4.0.1.Final:compile - omitted for duplicate)
如您所见,hibernate-core 自动导入 hibernate-commons-annotations。如果该版本与您在 pom 中包含的版本不同,Maven 将包含这两个版本,从而导致问题。除非您有充分的理由更改注释版本,否则最好让核心导入来完成它的工作。
如果你真的想从核心库中移除依赖,你可以通过添加<exclusions> 标签来实现。但要注意,因为 hibernate 套件的其他部分会尝试导入 annotations 包,您最终可能会从许多依赖项中排除 annotations。
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.0.1.Final</version>
<exclusions>
<exclusion> <!-- remove annotations inclusion -->
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>