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(
	at org.hibernate.internal.ExceptionConverterImpl.convert(
	at org.hibernate.query.internal.AbstractProducedQuery.list(
	at org.hibernate.query.internal.AbstractProducedQuery.getSingleResult(
	at [...]
	at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyProxyFactory.getProxy(
	at org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(
	at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(
	at org.hibernate.event.internal.DefaultLoadEventListener.createProxyIfNecessary(
	at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(
	at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(
	at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(
	at org.hibernate.internal.SessionImpl.fireLoad(
	at org.hibernate.internal.SessionImpl.internalLoad(
	at org.hibernate.type.EntityType.resolveIdentifier(
	at org.hibernate.type.EntityType.resolve(
	at org.hibernate.type.ManyToOneType.resolve(
	at org.hibernate.type.EntityType.resolve(
	at org.hibernate.type.ComponentType.resolve(
	at org.hibernate.type.Type.resolve(
	at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(
	at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(
	at org.hibernate.loader.Loader.initializeEntitiesAndCollections(
	at org.hibernate.loader.Loader.processResultSet(
	at org.hibernate.loader.Loader.doQuery(
	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(
	at org.hibernate.loader.Loader.doList(
	at org.hibernate.loader.Loader.doList(
	at org.hibernate.loader.Loader.listIgnoreQueryCache(
	at org.hibernate.loader.Loader.list(
	at org.hibernate.loader.hql.QueryLoader.list(
	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(
	at org.hibernate.engine.query.spi.HQLQueryPlan.performList(
	at org.hibernate.internal.SessionImpl.list(
	at org.hibernate.query.internal.AbstractProducedQuery.doList(
	at org.hibernate.query.internal.AbstractProducedQuery.list(
	... 4 more
Caused by: java.lang.InstantiationException: xxxxx-my-problematic-class-xxxxx$HibernateProxy$xJl4Zcl9
	at java.lang.Class.newInstance(
	at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyProxyFactory.getProxy(
	... 34 more
Caused by: java.lang.NoSuchMethodException: xxxxx-my-problematic-class-xxxxx$HibernateProxy$xJl4Zcl9.<init>()
	at java.lang.Class.getConstructor0(
	at java.lang.Class.newInstance(
	... 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 (

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


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: 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:

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