1

HIbernate JPA caused by incompatible with javassist.util.proxy.Proxy.

Code

public class EntityA {
    @Id
    private String id;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "entityB_id")
    private EntityB entityB;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "entityC_id")
    private EntityC entityC;
}

String sql = "select o from EntityA o "
        + "left outer join o.entityB as o1 ";
final TypedQuery<EntityA> query = getEm().createQuery(sql, EntityA.class);

final List<EntityA> result = query.getResultList();

Questions

When ran above query will get below exception, did anyone have an idea?

My query didn't involved the "EntityC", why the exception will throw with regarding the EntityC at getResultList() method?

Exception

Caused by: java.lang.ClassCastException: xxx.xx.xx.EntityC_$$_javassist_105 incompatible with javassist.util.proxy.Proxy
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.getProxy(JavassistLazyInitializer.java:148)
at org.hibernate.proxy.pojo.javassist.JavassistProxyFactory.getProxy(JavassistProxyFactory.java:73)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:758)
at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:4419)
at org.hibernate.event.internal.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:334)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:260)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1053)
at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:980)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:714)
at org.hibernate.type.EntityType.resolve(EntityType.java:502)
at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:168)
at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:137)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1112)
at org.hibernate.loader.Loader.processResultSet(Loader.java:969)
at org.hibernate.loader.Loader.doQuery(Loader.java:917)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:348)
at org.hibernate.loader.Loader.doList(Loader.java:2550)
at org.hibernate.loader.Loader.doList(Loader.java:2536)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2366)
at org.hibernate.loader.Loader.list(Loader.java:2361)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:495)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:357)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:198)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1230)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:268)
at sun.reflect.GeneratedMethodAccessor213.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:56)
at java.lang.reflect.Method.invoke(Method.java:620)
at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:368)
at com.sun.proxy.$Proxy153.getResultList(Unknown Source)
at com.cimb.module.payment.bill.ce.dao.CEMYCCPayeeAccessBean.findNormalByPayeeNameKeyword(CEMYCCPayeeAccessBean.java:59)
at com.cimb.module.payment.bill.atom.RetrieveBillerByKeywordMYAtom.performNormalBillerSearch(RetrieveBillerByKeywordMYAtom.java:58)
at com.cimb.module.payment.bill.atom.RetrieveBillerByKeywordMYAtom.proceedNextStep(RetrieveBillerByKeywordMYAtom.java:45)
at core.logic.DefaultLogicProcessor.process(DefaultLogicProcessor.java:78)
... 94 more
scott_lotus
  • 2,977
  • 19
  • 43
  • 66
zack
  • 9
  • 1
  • 6
  • Maybe you have a problem with your libraries on the class path. see http://stackoverflow.com/questions/22481540/hibernate-exception-javassist-0-cannot-be-cast-to-javassist-util-proxy-proxy – user140547 Jun 11 '16 at 06:43
  • I don't think is the libraries issue. I saw many post was regarding java.lang.ClassCastException: xxxx **cannot be cast to javassist.util.proxy.Proxy**, but not this **incompatible with javassist.util.proxy.Proxy**. It seem this associattion force me to join all the entities declare at the parent entity. – zack Jun 11 '16 at 06:52
  • Probably the library has different versions and may create another message for the `ClassCastException` depending on its version. However, it is still likely you are facing the same issue. – user140547 Jun 11 '16 at 07:16
  • Are you sure that the exception is caused by the `getResultList()` call? – ujulu Jun 11 '16 at 08:30
  • yes, pretty sure. It shown from the stack trace where is point the line number at this "final List result = query.getResultList();" – zack Jun 11 '16 at 09:03
  • @zack I face this issue. Getting from .getResultList();, How did you overcome? I use Hibernate 5.2.4 – Ratha Nov 09 '16 at 03:40
  • @Ratha For my case, because I'm using Websphere8.5 as AppServer, WAS8.5 bundle javassist lib inside the server cause it conflict with the Hibernate's javassist version. You can try by troubleshoot the classloader loading with your environment make sure no lib was conflict with the server jre or server runtime lib – zack Dec 05 '16 at 03:08

1 Answers1

0

Ok i have encountered this problem too. After some research here is my conclusion:

  1. javassist is too old or missing For older Spring version, you have to add / update your javassist. You can get the latest from the Maven repositorty. Link Here!

  2. If you are getting org.hibernate.LazyInitializationException: could not initialize proxy - no Session error, it is due to you are trying to do lazy load after your connection has been closed. You may need to add a @Transactional annotation in your service / controller (depending on your project's design), in either class level or method level.

BananZ
  • 991
  • 1
  • 8
  • 18