Hibernate ClassNotFoundException:org.hibernate.proxy.HibernateProxy

时间:2012-12-11 12:14:21

标签: hibernate osgi

我的hibernate设置抛出一些异常但仍然有效。为映射xml中定义的每个实体类抛出异常。我的项目使用equinox建立了OSGI。无法找到的类肯定在类路径中。

休眠配置

<?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.package.Driver</property>
        <property name="hibernate.connection.url">url</property>
        <property name="hibernate.connection.username">username</property>
        <property name="hibernate.connection.password">password</property>
        <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
        <property name="dialect">org.hibernate.dialect.H2Dialect</property>
        <property name="current_session_context_class">thread</property>
        <property name="hibernate.cache.use_second_level_cache">false</property>
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <property name="show_sql">false</property>
        <!-- C3P0 Connection Pool -->
        <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
        <property name="hibernate.c3p0.min_size">5</property>
        <property name="hibernate.c3p0.max_size">20</property>
        <property name="hibernate.c3p0.timeout">300</property>
        <property name="hibernate.c3p0.max_statements">50</property>
        <property name="hibernate.c3p0.idle_test_period">3000</property>
    </session-factory>
</hibernate-configuration>

异常

Javassist Enhancement failed: at.mkw.inlocs.common.Mapping
java.lang.RuntimeException: by java.lang.NoClassDefFoundError: org/hibernate/proxy/HibernateProxy
    at javassist.util.proxy.ProxyFactory.createClass3(ProxyFactory.java:344)
    at javassist.util.proxy.ProxyFactory.createClass2(ProxyFactory.java:314)
    at javassist.util.proxy.ProxyFactory.createClass(ProxyFactory.java:273)
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.getProxyFactory(JavassistLazyInitializer.java:163)
    at org.hibernate.proxy.pojo.javassist.JavassistProxyFactory.postInstantiate(JavassistProxyFactory.java:66)
    at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:187)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:170)
    at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:77)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:107)
    at org.hibernate.tuple.entity.EntityTuplizerFactory.constructDefaultTuplizer(EntityTuplizerFactory.java:135)
    at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(EntityEntityModeToTuplizerMapping.java:80)
    at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:323)
    at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:475)
    at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:133)
    at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:84)
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:295)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385)
    at at.mkw.inlocs.syncservice.client.LocalDatabase.configureHibernate(LocalDatabase.java:80)
    at at.mkw.inlocs.syncservice.client.LocalDatabase.login(LocalDatabase.java:153)
    at at.mkw.inlocs.swingclient.StartupHandler.createMainframe(StartupHandler.java:123)
    at at.mkw.inlocs.swingclient.Activator.runSwing(Activator.java:142)
    at at.mkw.inlocs.swingclient.Activator.access$6(Activator.java:71)
    at at.mkw.inlocs.swingclient.Activator$7.run(Activator.java:236)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: javassist.CannotCompileException: by java.lang.NoClassDefFoundError: org/hibernate/proxy/HibernateProxy
    at javassist.util.proxy.FactoryHelper.toClass(FactoryHelper.java:169)
    at javassist.util.proxy.ProxyFactory.createClass3(ProxyFactory.java:339)
    ... 40 more
Caused by: java.lang.NoClassDefFoundError: org/hibernate/proxy/HibernateProxy
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at javassist.util.proxy.FactoryHelper.toClass2(FactoryHelper.java:181)
    at javassist.util.proxy.FactoryHelper.toClass(FactoryHelper.java:163)
    ... 41 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.proxy.HibernateProxy
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:494)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:410)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:398)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:105)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 50 more

2 个答案:

答案 0 :(得分:6)

这是在OSGi环境中使用Hibernate的常见问题。问题是每个bundle都有自己的类加载器,当hibernate中使用Hibernate Class.forname()时会抛出异常,因为你的映射类不在hibernate类加载器的类路径上。在equinox中解决这个问题的方法是使用所谓的伙伴类加载。在包含你的休眠罐的捆绑包中,你添加了这个:

Eclipse-BuddyPolicy: registered

然后在带有映射类的bundle或bundle中,假设你的hibernate包的名称为org.hibernate

Eclipse-RegisterBuddy: org.hibernate

同样为了它的工作,您需要将hibernate包作为依赖项而不是通过导入的包添加,因此请确保将其添加到包含映射类的包中。

Require-Bundle: org.hibernate

了解更多信息google hibernate buddy

答案 1 :(得分:1)

并非所有问题都相同,即使它们都是类未找到异常。在我的场景中,使用Hibernate的持久性包具有org.hibernate.proxy类,但是具有hibernate类的bundle没有org.hibernate.proxy类。使用Import-Package导入这些类修复了我的问题

针对我的方案的问题的METE-INF更改

Import-Package:entity,javax.persistence; version =“[2.1,3)”,javax.xml.bin  d.annotation,org.hibernate.proxy,javassist.util.proxy