java.lang.ClassCastException: xut.bookshop.entity.User_$$_javassist_3 cannot be cast to javassist.util.proxy.Proxy

报错信息

java.lang.ClassCastException: xut.bookshop.entity.User_$$_javassist_3 cannot be cast to javassist.util.proxy.Proxy org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.getProxy(JavassistLazyInitializer.java:147) org.hibernate.proxy.pojo.javassist.JavassistProxyFactory.getProxy(JavassistProxyFactory.java:75) org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:771) org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:4613) org.hibernate.event.internal.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:350) org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:271) org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:151) org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1106) org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1025) org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:716) org.hibernate.type.EntityType.resolve(EntityType.java:502) org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:170) org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:144) org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.performTwoPhaseLoad(AbstractRowReader.java:244) org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:215) org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:140) org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:138) org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)

......

出现情况:使用hibernate进行多对一、一对多联系时出现该情况(多对多未测试)

举例:user表,order表,orderDetail表(具体到商品的订单)

一个用户可以有多个购买订单记录,一个订单可以有多个具体到单个商品的列表。

对order对象中加入一个user对象,一个set<orderDetail>,查询数据时报错java.lang.ClassCastException: xut.bookshop.entity.User_$$_javassist_3 cannot be cast to javassist.util.proxy.Proxy

解决:将这些对应关系的lazy属性设置为false

修改前:

修改后:

灵感来源

https://www.cnblogs.com/zhangzongle/p/5781682.html

 

posted @ 2019-04-26 20:32  Star灬木子李  阅读(290)  评论(0编辑  收藏  举报