12

好长啊!;-)

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

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

我的测试项目

我有一个小测试项目:

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

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

用户DAO.java

 public interface UserDAO {
    public void store(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

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

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

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

4

2 回答 2

4

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

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

<!-- optional deps for bytecode providers until those are finally properly scoped -->
于 2009-02-07T15:23:06.890 回答
2

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

dependency org="org.hibernate" name="hibernate-core" rev="3.3.1.GA" conf="runtime->runtime,optional"/>
于 2009-02-09T12:20:19.937 回答