Bytecode enhancement failed / NoSuchMethodException when lazy fetching a member

Hi, I’ve encountered a rather odd behaviour I can’t explain.

I’ve got a class that is referenced from various other classes via ManyToOne associations. Now I would like to load these Lazily. However, if I set the fetch type to lazy, I get an org.hibernate.HibernateException: HHH000142: Bytecode enhancement failed (stacktrace below). Eagerly is no problem. I can’t work out what I am doing wrong. Any ideas as to what the problem could be?

Thanks a lot!

Stack trace:

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.HibernateException: HHH000142: Bytecode enhancement failed: xxxxx-my-problematic-class-xxxxx
	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
	at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1514)
	at org.hibernate.query.internal.AbstractProducedQuery.getSingleResult(AbstractProducedQuery.java:1553)
	at [...]
	at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyProxyFactory.getProxy(ByteBuddyProxyFactory.java:97)
	at org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:701)
	at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:4791)
	at org.hibernate.event.internal.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:362)
	at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:277)
	at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:124)
	at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:92)
	at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1257)
	at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1140)
	at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:682)
	at org.hibernate.type.EntityType.resolve(EntityType.java:464)
	at org.hibernate.type.ManyToOneType.resolve(ManyToOneType.java:239)
	at org.hibernate.type.EntityType.resolve(EntityType.java:457)
	at org.hibernate.type.ComponentType.resolve(ComponentType.java:688)
	at org.hibernate.type.Type.resolve(Type.java:487)
	at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:172)
	at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:129)
	at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1151)
	at org.hibernate.loader.Loader.processResultSet(Loader.java:1010)
	at org.hibernate.loader.Loader.doQuery(Loader.java:948)
	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:340)
	at org.hibernate.loader.Loader.doList(Loader.java:2689)
	at org.hibernate.loader.Loader.doList(Loader.java:2672)
	at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2506)
	at org.hibernate.loader.Loader.list(Loader.java:2501)
	at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:504)
	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:395)
	at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:220)
	at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1508)
	at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1537)
	at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1505)
	... 4 more
Caused by: java.lang.InstantiationException: xxxxx-my-problematic-class-xxxxx$HibernateProxy$xJl4Zcl9
	at java.lang.Class.newInstance(Class.java:427)
	at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyProxyFactory.getProxy(ByteBuddyProxyFactory.java:90)
	... 34 more
Caused by: java.lang.NoSuchMethodException: xxxxx-my-problematic-class-xxxxx$HibernateProxy$xJl4Zcl9.<init>()
	at java.lang.Class.getConstructor0(Class.java:3082)
	at java.lang.Class.newInstance(Class.java:412)
	... 35 more

Make sure that the Hibernate version you are using is compatible with the ByteBuddy framework version.

You can check this info on GitHub on the branch you are using or by inspecting the Mven artifacts.

Thanks vlad for the quick reply!

The only dependency using ByteBuddy is Hibernate, so there shouldn’t be any other versions floating around (well and the now-used Maven enforcer plugin passes). I’m using Hibernate 5.3.6.Final and even tried to specifying the net.bytebuddy:byte-buddy:1.8.17 dependency manually in my dependencyManagement section. I still get this exception, I’ve also tried a couple of other Hibernate/ByteBuddy versions, all to no avail, unfortunately. (I assume Hibernate 5.3.6.Final is compatible with ByteBuddy 1.8.17 as that is the version provided in Hibernate’s pom.xml).

Do you have another idea as to what the issue could be?

Are you using the compile-time bytecode enhancement mechanism tool?

Maybe you are using and old version of the hibernate-enhance-maven-plugin.

No, I’m not currently using that plugin. (Should I?)

Do you have a public no-arg constructor in your entity?

Because it looks like it’s what is missing in the proxy (the proxy would expose it automatically if it was there).

That being said, if it’s the problem, we need to improve the error message.

1 Like

@j4r btw, I would be interested if you could reproduce this issue with our test case template (https://github.com/hibernate/hibernate-test-case-templates/tree/master/orm/hibernate-orm-5).

I’d like to take a look to what is happening exactly.

Thanks.

This was basically the issue, thank you! I did have a no-arg constructor but declared as private (I missed the fact that constructors - other than getters/setters - must be at least package-private). As soon as I declared the constructor as package-private it behaved as expected. An error message similar to the one when you forget a setter would be awesome for this scenario :).

Would you still like a test case, given the issue was pretty simple?

@j4r Thanks for your confirmation. I will try to create a replicating test case to see if I can replicate the issue and try to provide a more meaningful error message.

1 Like

@j4r I couldn’t replicate your use case. Please check out this commit and modify it so that you can replicate this error message. It worked just fine for me.

It believe it should be enough simply to add a private empty constructor to Country: https://github.com/hibernate/hibernate-orm/pull/2529. I am running into some other issues while trying to run the tests, so let me know if this is not sufficient. (Btw, directly fetching an instance of which the class has a private constructor from the database worked for me - just not loading it lazily when referred to in another class).

Ok, I will try with an empty constructor too and let you know.

Sorry for the delayed response. I got around to creating a quick ‘n’ dirty example project in which the issue exists: https://github.com/jjbr/hibernate-privConstr-Demo.

B contains a reference to A but A has a private constructor. As long as I don’t set the constructor to at least package-private I get mentioned Bytecode enhancement failed exception. Simply run the main method in the Main class.

Unfortunately this is not a test case but I believe it should be relatively easy to port, I just don’t have the time right now. But perhaps it is useful nevertheless - let me know!

Thanks. I created this Pull Request for this issue.

Do you have a public no-arg constructor in your entity?

This answer save my day

Hi,
Facing almost similar issue and this thread seems to be the most related to my problem.
Our project is on Groovy and using Hibernate 5.2.18.
It seems that Hibernate tries to call constructor from trait, but there is no constructor in traits in Groovy.
If I move fields declared in trait to every child class - problem goes away, but this decision is quite rude.

Appreciate for help!

SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: HHH000142: Bytecode enhancement failed: ***.***.**.**.***.Entity; nested exception is org.hibernate.HibernateException: HHH000142: Bytecode enhancement failed: ***.***.**.**.**.Entity] with root cause
java.lang.NullPointerException
	at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:38)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
	at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.call(PogoMetaMethodSite.java:78)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:139)
	at ***.***.**.**.***.Entity$Trait$Helper.$init$(Entity.groovy:71)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:107)
	at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite$StaticMetaMethodSiteNoUnwrapNoCoerce.invoke(StaticMetaMethodSite.java:149)
	at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.call(StaticMetaMethodSite.java:89)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
	at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.call(StaticMetaMethodSite.java:94)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:139)
	at ***.***.**.**.**.Entity.<init>(Entity.groovy:243)
	at ***.***.**.**.**.Entity$HibernateProxy$CdCH4eHy.<init>(Unknown Source)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyProxyFactory.getProxy(ByteBuddyProxyFactory.java:89)
	at org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:746)
	at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:5054)
	at org.hibernate.event.internal.DefaultLoadEventListener.createProxy(DefaultLoadEventListener.java:427)
	at org.hibernate.event.internal.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:418)
	at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:326)
	at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:108)
	at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:74)
	at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:104)
	at org.hibernate.internal.SessionImpl.fireLoadNoChecks(SessionImpl.java:1186)
	at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1051)
	at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:697)
	at org.hibernate.type.EntityType.resolve(EntityType.java:464)
	at org.hibernate.type.ManyToOneType.resolve(ManyToOneType.java:240)
	at org.hibernate.engine.internal.TwoPhaseLoad$EntityResolver.lambda$static$0(TwoPhaseLoad.java:605)
	at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntityEntryLoadedState(TwoPhaseLoad.java:248)
	at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:182)
	at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:151)
	at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.performTwoPhaseLoad(AbstractRowReader.java:247)
	at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:208)
	at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:96)
	at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:105)
	at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:285)
	at org.hibernate.persister.entity.AbstractEntityPersister.doLoad(AbstractEntityPersister.java:4442)
	at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4432)
	at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:569)
	at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:537)
	at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:208)
	at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:332)
	at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:108)
	at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:74)
	at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:104)
	at org.hibernate.internal.SessionImpl.fireLoadNoChecks(SessionImpl.java:1186)
	at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1175)
	at org.hibernate.internal.SessionImpl.access$2100(SessionImpl.java:193)
	at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.doLoad(SessionImpl.java:2786)
	at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.lambda$load$1(SessionImpl.java:2767)
	at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.perform(SessionImpl.java:2723)
	at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2767)
	at org.hibernate.internal.SessionImpl.find(SessionImpl.java:3322)
	at org.hibernate.internal.SessionImpl.find(SessionImpl.java:3289)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:366)
	at com.sun.proxy.$Proxy244.find(Unknown Source)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:314)
	at com.sun.proxy.$Proxy244.find(Unknown Source)
	at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findById(SimpleJpaRepository.java:281)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.data.repository.core.support.ImplementationInvocationMetadata.invoke(ImplementationInvocationMetadata.java:72)
	at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:382)
	at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:205)
	at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:550)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:155)
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:130)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:178)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
	at com.sun.proxy.$Proxy261.findById(Unknown Source)