4

我的休眠设置引发了一些异常,但仍然有效。映射 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
4

2 回答 2

6

这是在 OSGi 环境中使用 Hibernate 的常见问题。问题是每个包都有自己的类加载器,当Class.forname()在 hibernate 中使用 Hibernate 时,它​​会引发异常,因为您的映射类不在 hibernate 类加载器的类路径上。在 Equinox 中解决这个问题的方法是使用所谓的伙伴类加载。在包含 hibernate jar 的捆绑包中,您可以在其中添加以下内容:

Eclipse-BuddyPolicy: registered

然后在带有映射类的包中添加这个假设你的休眠包的名称org.hibernate

Eclipse-RegisterBuddy: org.hibernate

此外,要使其正常工作,您需要将休眠包添加为依赖项,而不是通过导入的包,因此请确保将其添加到您的包中以及映射类。

Require-Bundle: org.hibernate

欲了解更多信息谷歌休眠好友

于 2012-12-11T17:15:00.313 回答
1

并非所有问题都是相同的,即使它们都是类未发现异常。在我的场景中,使用 Hibernate 的持久性捆绑包具有 org.hibernate.proxy 类,但具有休眠类的捆绑包没有 org.hibernate.proxy 类。使用 Import-Package 导入这些类解决了我的问题

该问题的 METE-INF 更改适用于我的方案

导入包:entity,javax.persistence;version="[2.1,3)",javax.xml.bin d.annotation,org.hibernate.proxy,javassist.util.proxy

于 2017-02-15T21:56:40.643 回答