【问题标题】:hibernate-core & hibernate-annotation - conflictinghibernate-core & hibernate-annotation - 冲突
【发布时间】:2015-03-07 01:15:27
【问题描述】:

我收到错误:

java.lang.VerifyError: (class: org/hibernate/type/BasicTypeRegistry, method:  signature: ()V) Incompatible argument to function     
at org.hibernate.type.TypeResolver.(TypeResolver.java:59)   
at com.gs.ctt.cb.types.usertypes.GenericEnumUserType.setParameterValues(GenericEnumUserType.java:46) 
at org.hibernate.type.TypeFactory.injectParameters(TypeFactory.java:339)



由于可能的原因是库冲突,我发现该类:org.hibernate.cfg.AnnotationConfiguration 在两者中都可用:

  • hibernate-annotations-3.3.0
  • hibernate-core-3.6.4

我需要摆脱休眠注释吗?为什么?

【问题讨论】:

  • 我认为hibernate核心默认包含注解依赖。因此,在使用 maven 中包含注释将导致您的项目中有 2 个注释包。您可以从 hibernate-core 中排除 annotations 包,或者只是从项目中删除 annotations 依赖项。

标签: java hibernate hibernate-annotations verifyerror


【解决方案1】:

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 将包含这两个版本,从而导致问题。除非您有充分的理由更改注释版本,否则最好让核心导入来完成它的工作。


如果你真的想从核心库中移除依赖,你可以通过添加&lt;exclusions&gt; 标签来实现。但要注意,因为 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 2017-04-03
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    • 2018-09-13
    • 1970-01-01
    相关资源
    最近更新 更多