eclipse Hibernate-annotations 3.4.0.GA 和 slf4j?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3546911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 15:02:18  来源:igfitidea点击:

Hibernate-annotations 3.4.0.GA and slf4j?

eclipsehibernatemaven-2slf4j

提问by u123

I have a maven project A that use hibernate-annotations 3.4.0.GA which use the slf4j-api version 1.5.5 (checked through the dependency tree in the pom.xml file). Further project A specifies slf4j-log4j12 version 1.4.2 as a dependency.

我有一个 maven 项目 A,它使用 hibernate-annotations 3.4.0.GA,它使用 slf4j-api 1.5.5 版(通过 pom.xml 文件中的依赖树检查)。进一步的项目 A 指定 slf4j-log4j12 版本 1.4.2 作为依赖项。

I have another maven project B which depend on project A. In project B I have specified the following dependencies:

我有另一个依赖于项目 A 的 maven 项目 B。在项目 BI 中指定了以下依赖项:

slf4j-api version 1.6.1,
logback-core version 0.9.24
logback-classic version 0.9.24

which builds fine with maven from the command line. But when I run the project from eclipse launch configuration I get:

从命令行使用 maven 构建得很好。但是当我从 eclipse 启动配置运行项目时,我得到:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/mm/.m2/repository/org/slf4j/slf4j-log4j12/1.4.2/slf4j-log4j12-1.4.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/mm/.m2/repository/ch/qos/logback/logback-classic/0.9.24/logback-classic-0.9.24.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: slf4j-api 1.6.x (or later) is incompatible with this binding.
SLF4J: Your binding is version 1.5.5 or earlier.
SLF4J: Upgrade your binding to version 1.6.x. or 2.0.x

From this message its indicated to that I need to upgrade the binding in project A to 1.6.x, but I don't see how that is possible since its included in the hibernate dependency.

从这条消息它表明我需要将项目 A 中的绑定升级到 1.6.x,但我不知道这是怎么可能的,因为它包含在休眠依赖项中。

Is it possible to switch the binding (updating the classpath info) when running project B so it use the 1.6.1 version instead of the version from the hibernate project?

是否可以在运行项目 B 时切换绑定(更新类路径信息),以便使用 1.6.1 版本而不是休眠项目中的版本?

回答by Pascal Thivent

I have a maven project A that use hibernate-annotations 3.4.0.GA which use the slf4j-api version 1.5.5 (checked through the dependency tree in the pom.xml file). Further project A specifies slf4j-log4j12 version 1.4.2 as a dependency.

我有一个 maven 项目 A,它使用 hibernate-annotations 3.4.0.GA,它使用 slf4j-api 1.5.5 版(通过 pom.xml 文件中的依赖树检查)。进一步的项目 A 指定 slf4j-log4j12 版本 1.4.2 作为依赖项。

That's not recommended, you should use the same versions of slf4j artifacts. From the FAQ:

不建议这样做,您应该使用相同版本的 slf4j 工件。从常见问题:

Are SLF4J versions backward compatible?

With rare theoretical exceptions, SLF4J versions are backward compatible. This means than you can upgrade from SLF4J version 1.0 to any later version without problems.

However, while the SLF4J API is very stable from the client's perspective, SLF4J bindings such as slf4j-simple or slf4j-log4j12 may require a specific version of slf4j-api. Mixing different versions of slf4j artifacts can be problematic and is strongly discouraged.For instance, if you are using slf4j-api-1.5.6.jar, then you should also use slf4j-simple-1.5.6.jar, using slf4j-simple-1.4.2.jar will not work.

At initialization time, if SLF4J suspects that there may be a version mismatch problem, it emits a warning about the said mismatch. For the exact details of the version mismatch detection mechanism, please refer to the relevant entryin this FAQ.

SLF4J 版本是否向后兼容?

除了极少数的理论例外,SLF4J 版本向后兼容。这意味着您可以毫无问题地从 SLF4J 1.0 版升级到任何更高版本。

然而,虽然从客户端的角度来看 SLF4J API 非常稳定,但 SLF4J 绑定(例如 slf4j-simple 或 slf4j-log4j12)可能需要特定版本的 slf4j-api。混合不同版本的 slf4j 工件可能会出现问题,强烈建议不要这样做。例如,如果您使用的是 slf4j-api-1.5.6.jar,那么您也应该使用 slf4j-simple-1.5.6.jar,使用 slf4j-simple-1.4.2.jar 将不起作用。

在初始化时,如果 SLF4J 怀疑可能存在版本不匹配问题,它会发出关于所述不匹配的警告。有关版本不匹配检测机制的确切详细信息,请参阅此 FAQ 中的相关条目

That's something to fix.

这是要解决的问题。

which builds fine with maven from the command line. But when I run the project from eclipse launch configuration I get (...)

从命令行使用 maven 构建得很好。但是当我从 eclipse 启动配置运行项目时,我得到 (...)

The problem is that you get the SLF4J artifacts from B andthose from A (transitively) and you thus end up mixing several versions of the slf4j-api(1.5.5 and 1.6.1) and several bindings(slf4j-log4j12and logback-classic). SLF4J is complaining about the later problem at runtime but you should fix both.

问题是您从 BA(可传递)获得了 SLF4J 工件,因此您最终混合slf4j-api(1.5.5 和 1.6.1)和几个绑定slf4j-log4j12logback-classic)的几个版本。SLF4J 在运行时抱怨后面的问题,但您应该同时解决这两个问题。

From this message its indicated to that I need to upgrade the binding in project A to 1.6.x, but I don't see how that is possible since its included in the hibernate dependency.

从这条消息它表明我需要将项目 A 中的绑定升级到 1.6.x,但我不知道这是怎么可能的,因为它包含在休眠依赖项中。

Yes, the message suggests upgrading a binding to a more recent version. But, more important, it reports that you have more than ONEbinding on the class path: you need to choose between log4j and logback as logging backed and to provide the appropriate binding, but not both of them.

是的,该消息建议将绑定升级到更新的版本。但是,更重要的是,报告有超过类路径上绑定:你需要为日志备份和Log4j和的logback之间进行选择,以提供相应的约束力,但不是他们两个。

Is it possible to switch the binding (updating the classpath info) when running project B so it use the 1.6.1 version instead of the version from the hibernate project?

是否可以在运行项目 B 时切换绑定(更新类路径信息),以便使用 1.6.1 版本而不是休眠项目中的版本?

To strictly answer this question about controlling versions in transitive dependencies, this can be done using the dependencyManagementelement. Here is an example:

要严格回答有关在传递依赖项中控制版本的问题,可以使用dependencyManagement元素来完成。下面是一个例子:

<project>
  ...
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.1</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
  ...
  <dependencies>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>3.4.0.GA</version>
    </dependency>
  </dependencies>
  ...
</project>

And artifacts having slf4j-apias dependency, such as Hibernate EntityManager, would use version 1.6.1 as shown below:

具有slf4j-api依赖项的工件,例如 Hibernate EntityManager,将使用 1.6.1 版本,如下所示:

$ mvn dependency:tree
...
[INFO] +- org.hibernate:hibernate-entitymanager:jar:3.4.0.GA:compile
[INFO] |  +- org.hibernate:ejb3-persistence:jar:1.0.2.GA:compile
[INFO] |  +- org.hibernate:hibernate-commons-annotations:jar:3.1.0.GA:compile
[INFO] |  +- org.hibernate:hibernate-annotations:jar:3.4.0.GA:compile
[INFO] |  +- org.hibernate:hibernate-core:jar:3.3.0.SP1:compile
[INFO] |  |  +- antlr:antlr:jar:2.7.6:compile
[INFO] |  |  \- commons-collections:commons-collections:jar:3.1:compile
[INFO] |  +- org.slf4j:slf4j-api:jar:1.6.1:compile
[INFO] |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  |  \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] |  +- javax.transaction:jta:jar:1.1:compile
[INFO] |  \- javassist:javassist:jar:3.4.GA:compile

But as I said, the real problem is that you need to have only one binding on the classpath. Either choose log4j or logback, just not both, and provide the appropriate binding.

但正如我所说,真正的问题是您只需要在类路径上绑定一个。选择 log4j 或 logback,不要同时选择两者,并提供适当的绑定。

回答by samspot

I had this problem but after crawling my dependency tree and fixing my pom I was still having issues. My solution?

我遇到了这个问题,但是在爬行我的依赖树并修复了我的 pom 之后,我仍然遇到了问题。我的解决方案?

mvn clean

(just in case anyone else made my mistake!)

(以防万一其他人犯了我的错误!)