Hibernate ClassNotFoundException: org.hibernate.proxy.HibernateProxy
Posted
技术标签:
【中文标题】Hibernate ClassNotFoundException: org.hibernate.proxy.HibernateProxy【英文标题】: 【发布时间】:2012-11-29 00:04:18 【问题描述】:我的休眠设置引发了一些异常,但仍然有效。映射 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
【问题讨论】:
这可能是相关的:hibernate.onjira.com/browse/HHH-3826 【参考方案1】:这是在 OSGi 环境中使用 Hibernate 的常见问题。问题是每个包都有自己的类加载器,当 Hibernate Class.forname()
在 hibernate 中使用时,它会引发异常,因为您的映射类不在 hibernate 类加载器的类路径上。在Equinox中解决这个问题的方法是使用所谓的伙伴类加载。在包含您的休眠罐子的捆绑包中,您可以在其中添加以下内容:
Eclipse-BuddyPolicy: registered
然后在具有映射类的包中添加这个假设你的休眠包的名称org.hibernate
Eclipse-RegisterBuddy: org.hibernate
此外,要使其正常工作,您需要将 hibernate 捆绑包添加为依赖项,而不是通过导入的包,因此请确保将其添加到您的捆绑包中以及映射类。
Require-Bundle: org.hibernate
更多信息谷歌休眠好友
【讨论】:
【参考方案2】:并非所有问题都是相同的,即使它们都是类未找到异常。在我的场景中,使用 Hibernate 的持久性捆绑包具有 org.hibernate.proxy 类,但具有休眠类的捆绑包没有 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
【讨论】:
添加Import-Package: org.hibernate.proxy, javassist.util.proxy
也为我解决了这个问题。不过,我没有添加 entity, javax.persistence;version="[2.1,3)", javax.xml.bin.annotation
导入。以上是关于Hibernate ClassNotFoundException: org.hibernate.proxy.HibernateProxy的主要内容,如果未能解决你的问题,请参考以下文章
什么可能导致长时间运行的进程中突然出现 ClassNotFoundException?
在 Android 应用中,Google 登录弹出窗口未加载
Hibernate→ 《Hibernate程序开发》教材大纲