使用Apache Ivy无法获得Hibernate Core和Hibernate Annotations(但它也与Maven2相关。)

时间:2009-02-06 16:09:33

标签: java hibernate maven-2 annotations ivy

这是一个很长的! ; - )

这个问题中有很多复制粘贴的文字使它看起来 复杂。说实话,这是很多信息!然而,对于有经验的人来说,很多可能是不必要的,也很容易撇去。

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

我的测试项目

我有一个小型测试项目:

hibernate.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

 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

 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.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

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

User.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;
    }
 }

运行测试项目时遇到的异常:

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$1.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

如果它缺少依赖

当我得到 ClassCastException (它包含在上面)时,我认为它会是一个 好的想法,检查我是否有所有依赖。

如果我下载 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

如果我下载 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

默认我认为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>

依赖 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

POM-file for hibernate-core on ibiblio中有一些行 我想知道:

   <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>

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

2 个答案:

答案 0 :(得分:4)

我明确地将项目中的javassist指定为依赖项,并告诉它是否有效。

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

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

答案 1 :(得分:2)

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

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