Java 11:OSGi:生成 EntityManager,无法构建 Hibernate SessionFactory java.lang.ClassNotFoundException:org.hib
Posted
技术标签:
【中文标题】Java 11:OSGi:生成 EntityManager,无法构建 Hibernate SessionFactory java.lang.ClassNotFoundException:org.hibernate.proxy.HibernateProxy【英文标题】:Java 11: OSGi: Spawning EntityManager, Unable to build Hibernate SessionFactory java.lang.ClassNotFoundException: org.hibernate.proxy.HibernateProxy 【发布时间】:2022-01-17 21:06:55 【问题描述】:我正在将 Java 8 项目转换为 Java 11,作为转换的一部分,我进行了一系列不同的代码更改和升级(最值得注意的是从 4.x 升级到 Hibernate 5.3.18.FINAL。 X)。我已经让整个大型项目在 Java 11 中成功编译。但是,当我使用 Karaf 启动项目时,我遇到了一个持续的 OSGi 错误,这让我发疯了。
[FelixDispatchQueue] ERROR org.osgi.framework.FrameworkEvent - FrameworkEvent ERROR
javax.persistence.PersistenceException: [PersistenceUnit: abc] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1327) ~[?:?]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1253) ~[?:?]
at org.hibernate.osgi.OsgiPersistenceProvider.createContainerEntityManagerFactory(OsgiPersistenceProvider.java:96) ~[?:?]
at org.apache.aries.jpa.container.impl.AriesEntityManagerFactoryBuilder.createAndPublishEMF(AriesEntityManagerFactoryBuilder.java:371) ~[?:?]
at org.apache.aries.jpa.container.impl.AriesEntityManagerFactoryBuilder.createEntityManagerFactory(AriesEntityManagerFactoryBuilder.java:183) ~[?:?]
at org.apache.aries.jpa.container.impl.AriesEntityManagerFactoryBuilder.dataSourceReady(AriesEntityManagerFactoryBuilder.java:538) ~[?:?]
at org.apache.aries.jpa.container.impl.AriesEntityManagerFactoryBuilder.foundDS(AriesEntityManagerFactoryBuilder.java:510) ~[?:?]
at org.apache.aries.jpa.container.impl.DataSourceTracker.addingService(DataSourceTracker.java:86) ~[?:?]
at org.apache.aries.jpa.container.impl.DataSourceTracker.addingService(DataSourceTracker.java:34) ~[?:?]
at org.osgi.util.tracker.ServiceTracker$Tracked.customizerAdding(ServiceTracker.java:941) ~[osgi.core-6.0.0.jar:?]
at org.osgi.util.tracker.ServiceTracker$Tracked.customizerAdding(ServiceTracker.java:870) ~[osgi.core-6.0.0.jar:?]
at org.osgi.util.tracker.AbstractTracked.trackAdding(AbstractTracked.java:256) ~[osgi.core-6.0.0.jar:?]
at org.osgi.util.tracker.AbstractTracked.trackInitial(AbstractTracked.java:183) ~[osgi.core-6.0.0.jar:?]
at org.osgi.util.tracker.ServiceTracker.open(ServiceTracker.java:318) ~[osgi.core-6.0.0.jar:?]
at org.osgi.util.tracker.ServiceTracker.open(ServiceTracker.java:261) ~[osgi.core-6.0.0.jar:?]
at org.apache.aries.jpa.container.impl.AriesEntityManagerFactoryBuilder.<init>(AriesEntityManagerFactoryBuilder.java:99) ~[?:?]
at org.apache.aries.jpa.container.impl.PersistenceProviderTracker.addingService(PersistenceProviderTracker.java:88) ~[?:?]
at org.apache.aries.jpa.container.impl.PersistenceProviderTracker.addingService(PersistenceProviderTracker.java:44) ~[?:?]
at org.osgi.util.tracker.ServiceTracker$Tracked.customizerAdding(ServiceTracker.java:941) ~[osgi.core-6.0.0.jar:?]
at org.osgi.util.tracker.ServiceTracker$Tracked.customizerAdding(ServiceTracker.java:870) ~[osgi.core-6.0.0.jar:?]
at org.osgi.util.tracker.AbstractTracked.trackAdding(AbstractTracked.java:256) ~[osgi.core-6.0.0.jar:?]
at org.osgi.util.tracker.AbstractTracked.trackInitial(AbstractTracked.java:183) ~[osgi.core-6.0.0.jar:?]
at org.osgi.util.tracker.ServiceTracker.open(ServiceTracker.java:318) ~[osgi.core-6.0.0.jar:?]
at org.osgi.util.tracker.ServiceTracker.open(ServiceTracker.java:261) ~[osgi.core-6.0.0.jar:?]
at org.apache.aries.jpa.container.impl.PersistenceBundleTracker.trackProvider(PersistenceBundleTracker.java:229) ~[?:?]
at org.apache.aries.jpa.container.impl.PersistenceBundleTracker.findPersistenceUnits(PersistenceBundleTracker.java:213) ~[?:?]
at org.apache.aries.jpa.container.impl.PersistenceBundleTracker.addingBundle(PersistenceBundleTracker.java:98) ~[?:?]
at org.apache.aries.jpa.container.impl.PersistenceBundleTracker.addingBundle(PersistenceBundleTracker.java:44) ~[?:?]
at org.osgi.util.tracker.BundleTracker$Tracked.customizerAdding(BundleTracker.java:469) ~[osgi.core-6.0.0.jar:?]
at org.osgi.util.tracker.BundleTracker$Tracked.customizerAdding(BundleTracker.java:415) ~[osgi.core-6.0.0.jar:?]
at org.osgi.util.tracker.AbstractTracked.trackAdding(AbstractTracked.java:256) ~[osgi.core-6.0.0.jar:?]
at org.osgi.util.tracker.AbstractTracked.track(AbstractTracked.java:229) ~[osgi.core-6.0.0.jar:?]
at org.osgi.util.tracker.BundleTracker$Tracked.bundleChanged(BundleTracker.java:444) ~[osgi.core-6.0.0.jar:?]
at org.apache.felix.framework.EventDispatcher.invokeBundleListenerCallback(EventDispatcher.java:915) ~[?:?]
at org.apache.felix.framework.EventDispatcher.fireEventImmediately(EventDispatcher.java:834) ~[?:?]
at org.apache.felix.framework.EventDispatcher.fireBundleEvent(EventDispatcher.java:516) ~[?:?]
at org.apache.felix.framework.Felix.fireBundleEvent(Felix.java:4579) ~[?:?]
at org.apache.felix.framework.Felix.activateBundle(Felix.java:2231) ~[?:?]
at org.apache.felix.framework.Felix.startBundle(Felix.java:2146) ~[?:?]
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:998) ~[?:?]
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:984) ~[?:?]
at org.apache.karaf.features.internal.service.BundleInstallSupportImpl.startBundle(BundleInstallSupportImpl.java:165) ~[?:?]
at org.apache.karaf.features.internal.service.FeaturesServiceImpl.startBundle(FeaturesServiceImpl.java:1154) ~[?:?]
at org.apache.karaf.features.internal.service.Deployer.deploy(Deployer.java:1044) ~[?:?]
at org.apache.karaf.features.internal.service.FeaturesServiceImpl.doProvision(FeaturesServiceImpl.java:1063) ~[?:?]
at org.apache.karaf.features.internal.service.FeaturesServiceImpl.lambda$doProvisionInThread$13(FeaturesServiceImpl.java:998) ~[?:?]
at java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[?:?]
at java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[?:?]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[?:?]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[?:?]
at java.lang.Thread.run(Thread.java:829) [?:?]
...
...
Caused by: java.lang.NoClassDefFoundError: org/hibernate/proxy/HibernateProxy
at java.lang.ClassLoader.defineClass1(Native Method) ~[?:?]
at java.lang.System$2.defineClass(System.java:2131) ~[?:?]
at java.lang.invoke.MethodHandles$Lookup.defineClass(MethodHandles.java:962) ~[?:?]
at jdk.internal.reflect.GeneratedMethodAccessor27.invoke(Unknown Source) ~[?:?]
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]
at net.bytebuddy.dynamic.loading.ClassInjector$UsingLookup$Dispatcher$ForJava9CapableVm.defineClass(ClassInjector.java:1685) ~[?:?]
at net.bytebuddy.dynamic.loading.ClassInjector$UsingLookup.injectRaw(ClassInjector.java:1418) ~[?:?]
at net.bytebuddy.dynamic.loading.ClassInjector$AbstractBase.inject(ClassInjector.java:112) ~[?:?]
at net.bytebuddy.dynamic.loading.ClassLoadingStrategy$UsingLookup.load(ClassLoadingStrategy.java:466) ~[?:?]
at net.bytebuddy.dynamic.TypeResolutionStrategy$Passive.initialize(TypeResolutionStrategy.java:100) ~[?:?]
at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:5662) ~[?:?]
at org.hibernate.bytecode.internal.bytebuddy.ByteBuddyState.lambda$load$0(ByteBuddyState.java:187) ~[?:?]
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:152) ~[?:?]
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:365) ~[?:?]
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:174) ~[?:?]
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:376) ~[?:?]
at org.hibernate.bytecode.internal.bytebuddy.ByteBuddyState.load(ByteBuddyState.java:183) ~[?:?]
at org.hibernate.bytecode.internal.bytebuddy.ByteBuddyState.loadProxy(ByteBuddyState.java:107) ~[?:?]
at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyProxyHelper.buildProxy(ByteBuddyProxyHelper.java:54) ~[?:?]
at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyProxyFactory.postInstantiate(ByteBuddyProxyFactory.java:62) ~[?:?]
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:103) ~[?:?]
at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:155) ~[?:?]
at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:51) ~[?:?]
at jdk.internal.reflect.GeneratedConstructorAccessor59.newInstance(Unknown Source) ~[?:?]
at jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:?]
at java.lang.reflect.Constructor.newInstance(Constructor.java:490) ~[?:?]
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:88) ~[?:?]
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructDefaultTuplizer(EntityTuplizerFactory.java:116) ~[?:?]
at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:413) ~[?:?]
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:557) ~[?:?]
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:124) ~[?:?]
at jdk.internal.reflect.GeneratedConstructorAccessor58.newInstance(Unknown Source) ~[?:?]
at jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:?]
at java.lang.reflect.Constructor.newInstance(Constructor.java:490) ~[?:?]
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:96) ~[?:?]
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:77) ~[?:?]
at org.hibernate.metamodel.internal.MetamodelImpl.initialize(MetamodelImpl.java:154) ~[?:?]
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:295) ~[?:?]
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:467) ~[?:?]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1250) ~[?:?]
... 48 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.proxy.HibernateProxy
在你问之前,我已经将匹配的 bytebuddy 和 javassist 的每一个组合都放在了 pom 文件中(匹配 5.3.18.FINAL 所需的版本),但这并不能解决问题。请注意,我的项目在 Maven 中成功构建,但是当我使用 karaf 启动它时根本找不到 HibernateProxy。降级到 Java 8 不是一种选择 - 这需要在 Java 11 中运行。
更多技术细节: 使用 Spring Boot 使用蓝图 Hibernate.xml 更新为 persistence_2_1.xsd
<jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=abc)</jta-data-source>
数据库是h2
【问题讨论】:
【参考方案1】:虽然这不是一个完美的解决方案,但我能够暂时解决这个问题的方法是在主类路径中强制使用 hibernate-core jar。我确定主要问题是 hibernate-core 不在正确的位置,但我不清楚如何。我对 pom 进行了很多操作并为 maven-bundle-plugin 添加了异常,但这不起作用 - 只是将 hibernate-core 放在主类路径中。
【讨论】:
以上是关于Java 11:OSGi:生成 EntityManager,无法构建 Hibernate SessionFactory java.lang.ClassNotFoundException:org.hib的主要内容,如果未能解决你的问题,请参考以下文章
OSGi 和 Java EE 之间的根本区别是啥? [关闭]
如何使用 bndtools 通过 Scala 创建 OSGi 包?