Java: exception when casting to itself(?)

╄→尐↘猪︶ㄣ 提交于 2019-12-12 15:09:01

问题


Im working with Oracle WebLogic, Netbeans 6.9.1. I have the following lines of code:

Query query=entityManager.createNamedQuery("Items.findById").setParameter("id", Integer.parseInt((String) request.getParameter("id")));
Items it=(Items) query.getSingleResult();

and they throw an exception:

java.lang.ClassCastException: entity.Items cannot be cast to entity.Items

Even if that seem impossible, i saw similar question on SO: here Author havent published the solution but one of participants made a good asumption about different class loaders.

Meanwhile if i use getResultList and then work with it in JSP, it works fine. But i cant make casting work in servlet code.

How can i fix this in weblogic?


回答1:


It doesn't solve your problem specifically, but if you're using WebLogic Server 10.3.x or thereabouts, there is a web application provided as part of that distribution that you can use to troubleshooting classloading issues.

You can read more about it here: http://download.oracle.com/docs/cd/E17904_01/web.1111/e13706/classloading.htm#WLPRG495

It should be compatible with earlier versions of WebLogic Server, so you could use that in conjunction with the proposed workarounds to get to a solution.




回答2:


See this similiar question with a proposed workaround.




回答3:


This indeed looks like a classloading problem.

This earlier answer of mine explains how to verify this assumption.

Unfortunately, classloading problems are difficult and painful. There is no easy fix, only to try to understand what classloaders are loading what classes in which order, and trying to play around with your classloaders, classes and jars to eliminate the problem. Also, analyse the most recent changes in your app before this error appeared - these can provide the key to understanding the root cause.

I am not familiar with the classloading scheme in WebLogic, so unfortunately I can't give you any more specific advice.



来源:https://stackoverflow.com/questions/5578797/java-exception-when-casting-to-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!