Bytecode enhancement failed / NoSuchMethodException when lazy fetching a member


#1

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

#2

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.


#3

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?


#4

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

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


#5

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


#6

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.


#7

@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.


#8

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?


#9

@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.


#10

@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.


#11

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).


#12

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


#13

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!


#14

Thanks. I created this Pull Request for this issue.