Java 使用 Apache Ivy 获取 Hibernate Core 和 Hibernate Annotations 时遇到问题(但它也与 Maven2 相关。)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/520902/
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-08-11 15:44:44  来源:igfitidea点击:

Trouble getting Hibernate Core and Hibernate Annotations using Apache Ivy (but it's also Maven2 related.)

javahibernatemaven-2annotationsivy

提问by

It's a long one! ;-)

好长啊!;-)

There are a lot of copy pasted text in this question which makes it look complicated. And to be honest, it's a lot of information! However to an experienced person a lot of it might be unnecessary and easy to skim over.

这个问题中有很多复制粘贴的文本,这使它看起来很复杂。老实说,这是很多信息!然而,对于有经验的人来说,其中很多可能是不必要的,而且很容易浏览。

In its essence I'm only wondering why my basic Hibernate Core + Hibernate Annotationstest project doesn't work. I suspect missing dependencies, but I'm using Apache Ivy which I thought would get transitive dependencies automatically from the Maven2 repository ibiblio, but there clearly are dependencies missing (see headline lower in the post about dependencies).

从本质上讲,我只是想知道为什么我的基本Hibernate Core + Hibernate Annotations测试项目不起作用。我怀疑缺少依赖项,但我正在使用 Apache Ivy,我认为它会自动从 Maven2 存储库 ibiblio 获取传递依赖项,但显然缺少依赖项(请参阅关于依赖项的帖子下方的标题)。

My test project

我的测试项目

I have a small test project:

我有一个小测试项目:

hibernate.cfg.xml

休眠文件.cfg.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE hibernate-configuration PUBLIC
         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 <hibernate-configuration>
     <session-factory>
         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
         <property name="connection.url">jdbc:mysql://localhost:3306/webcookieschema</property>
         <property name="connection.username">webcookieuser</property>
         <property name="connection.password">tHePaSsWoRd</property>
         <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
         </session-factory>
 </hibernate-configuration>

Test.java

测试.java

 public class Test {
    public static void main(String[] args) {
        UserDAO udao = new HibernateUserDAO();
        User u = new User();
        u.setName("somename");
        udao.store(u);
    }
 }

HibernateUserDAO.java

HibernateUserDAO.java

 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 import org.hibernate.Transaction;
 import org.hibernate.cfg.AnnotationConfiguration;

 public class HibernateUserDAO implements
        UserDAO {

    private SessionFactory sessionFactory;

    public HibernateUserDAO() {
        AnnotationConfiguration annotConf = new AnnotationConfiguration();
        annotConf.addAnnotatedClass(User.class);
        annotConf.configure();
        // The line below generates the exception!
        sessionFactory = annotConf.buildSessionFactory();
    }

    @Override
    public void store(User user) {
        Session session = sessionFactory.openSession();
        Transaction tx = session.getTransaction();
        try {
            tx.begin();
            session.saveOrUpdate(user);
            tx.commit();
        } catch (RuntimeException e) {
            tx.rollback();
            throw e;
        } finally {
            session.close();
        }
    }
 }

log4j.properties

log4j.properties

 log4j.appender.Stdout=org.apache.log4j.ConsoleAppender
 log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout
 log4j.appender.Stdout.layout.conversionPattern=%-5p - %-26.26c{1} - %m\n

 log4j.rootLogger=INFO,Stdout

 log4j.logger.org.apache.wicket=INFO
 log4j.logger.org.apache.wicket.protocol.http.HttpSessionStore=INFO
 log4j.logger.org.apache.wicket.version=INFO
 log4j.logger.org.apache.wicket.RequestCycle=INFO

UserDAO.java

UserDAO.java

 public interface UserDAO {
    public void store(User user);
 }

User.java

用户.java

 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
 import javax.persistence.Id;

 @Entity
 public class User {
    @Id
    @GeneratedValue
    @Column(name = "ID")
    private Long id;

    @Column(name = "NAME")
    private String name;

    public User() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
 }

The exception I get when I run the test project:

我得到的例外,当我运行测试项目:

Exception in thread "main" java.lang.NoClassDefFoundError: javassist/util/proxy/MethodFilter
at org.hibernate.bytecode.javassist.BytecodeProviderImpl.getProxyFactoryFactory(BytecodeProviderImpl.java:49)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactoryInternal(PojoEntityTuplizer.java:203)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:181)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:158)
at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:76)
at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(EntityEntityModeToTuplizerMapping.java:80)
at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:325)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:457)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:131)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:84)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:261)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1327)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
at HibernateUserDAO.<init>(HibernateUserDAO.java:15)
at Test.main(Test.java:3)
Caused by: java.lang.ClassNotFoundException:
    javassist.util.proxy.MethodFilter
    at java.net.URLClassLoader.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    ... 15 more


If it's missing dependencies

如果缺少依赖项

As I get a ClassCastException(it's included above) I thought it would be a good idea to check that I have all dependencies.

当我收到ClassCastException(它包含在上面)时,我认为检查我是否拥有所有依赖项是个好主意。

If I download the Hibernate Corearchive and look in the /lib directory of the archive I get:

如果我下载Hibernate Core存档并查看存档的 /lib 目录,我会得到:

kent@rat:~/dl-web/hibernate-distribution-3.3.1.GA$ tree lib/
lib/
|-- bytecode
|   |-- cglib
|   |   `-- hibernate-cglib-repack-2.1_3.jar
|   `-- javassist
|       `-- javassist-3.4.GA.jar
|-- optional
|   |-- c3p0
|   |   `-- c3p0-0.9.1.jar
|   |-- ehcache
|   |   `-- ehcache-1.2.3.jar
|   |-- jbosscache
|   |   `-- jboss-cache-1.4.1.GA.jar
|   |-- jbosscache2
|   |   `-- jbosscache-core-2.1.1.GA.jar
|   |-- oscache
|   |   `-- oscache-2.1.jar
|   |-- proxool
|   |   `-- proxool-0.8.3.jar
|   `-- swarmcache
|       `-- swarmcache-1.0RC2.jar
`-- required
    |-- antlr-2.7.6.jar
    |-- commons-collections-3.1.jar
    |-- dom4j-1.6.1.jar
    |-- javassist-3.4.GA.jar
    |-- jta-1.1.jar
    `-- slf4j-api-1.5.2.jar

If I download the Hibernate Annotationsand do the same thing I get:

如果我下载Hibernate Annotations并做同样的事情,我会得到:

kent@rat:~/dl-web/hibernate-annotations-3.4.0.GA$ tree lib/
lib/
|-- README.txt
|-- build
|   |-- ant-contrib-1.0b2.jar
|   |-- ant-junit-1.6.5.jar
|   `-- junit-3.8.1.jar
|-- dom4j.jar
|-- ejb3-persistence.jar
|-- hibernate-commons-annotations.jar
|-- hibernate-core.jar
|-- slf4j-api.jar
`-- test
    |-- antlr.jar
    |-- asm-attrs.jar
    |-- asm.jar
    |-- commons-collections.jar
    |-- javassist.jar
    |-- jta.jar
    |-- junit.jar
    |-- log4j.jar
    `-- slf4j-log4j12.jar

By default I think Ivy uses the ibiblio servers in Maven2 mode to retrieve its dependencies. I'm using this ivy.xml to configure which dependencies I want:

默认情况下,我认为 Ivy 在 Maven2 模式下使用 ibiblio 服务器来检索其依赖项。我正在使用这个ivy.xml 来配置我想要的依赖项:

<?xml version="1.0" encoding="UTF-8"?>
<ivy-module version="2.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd">
    <info
      organisation="testorganization"
      module="hibernatetest"
      status="integration"/>
    <configurations>
        <conf name="runtime" description="" />
    </configurations>
    <dependencies>
        <dependency org="mysql" name="mysql-connector-java" rev="5.1.6" conf="runtime->default"/>
        <dependency org="org.hibernate" name="hibernate-core" rev="3.3.1.GA" conf="runtime->default"/>
        <dependency org="org.hibernate" name="hibernate-annotations" rev="3.4.0.GA" conf="runtime->default">
            <include></include>
        </dependency>
        <dependency org="org.slf4j" name="slf4j-log4j12" rev="1.5.6" conf="runtime->default"/>
    </dependencies>
</ivy-module>

The dependency JAR's I actually getare:

我实际得到的依赖JAR是:

kent@rat:~/.ivy2/cache$ ls -R1 * | egrep '.jar$'
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
jta-1.1.jar
log4j-1.2.14.jar
mysql-connector-java-5.1.6.jar
ejb3-persistence-1.0.2.GA.jar
hibernate-annotations-3.4.0.GA.jar
hibernate-commons-annotations-3.1.0.GA.jar
hibernate-core-3.3.1.GA.jar
slf4j-api-1.5.6.jar
slf4j-log4j12-1.5.6.jar
xml-apis-1.0.b2.jar

In the POM-file for hibernate-core on ibibliothere are some lines which I'm wondering about:

ibiblio 上 hibernate-corePOM 文件中有一些我想知道的行:

   <dependencies>
       ...
       ...
       ...
       <!-- optional deps for bytecode providers until those are finally properly scoped -->
       <dependency>
           <groupId>javassist</groupId>
           <artifactId>javassist</artifactId>
           <version>3.4.GA</version>
           <optional>true</optional>
       </dependency>
       <dependency>
           <groupId>org.hibernate</groupId>
           <artifactId>hibernate-cglib-repack</artifactId>
           <version>2.1_3</version>
           <optional>true</optional>
       </dependency>
   </dependencies>

What do they mean? Do I need both of them? Why are they set as optional if they are really always required? And how do I get them?

他们的意思是什么?我需要他们两个吗?如果真的总是需要它们,为什么将它们设置为可选?我如何获得它们?

采纳答案by Hyman Leow

I had to add ,optionalto my Ivy.xml to also get the optional configuration, which was necessary for all dependencies to be retrieved and for the project run correctly.

我必须在Ivy.xml 中添加,optional以获取可选配置,这是检索所有依赖项和正确运行项目所必需的。

dependency org="org.hibernate" name="hibernate-core" rev="3.3.1.GA" conf="runtime->runtime,optional"/>

回答by Hyman Leow

I'd explicitly specify javassist in your project as a dependency and seee if that works.

我会在您的项目中明确指定 javassist 作为依赖项,看看是否有效。

As to why it's listed as optional, there are several reasons someone would do that, but in this case, it looks like the Hibernate team just hasn't decided on the actual dependency scope:

至于为什么它被列为可选,有几个原因有人会这样做,但在这种情况下,Hibernate 团队似乎还没有决定实际的依赖范围:

<!-- optional deps for bytecode providers until those are finally properly scoped -->