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: <a href="https://stackoverflow.com/questions/2233001/new-to-java-entities-entity-class-complaining-of-being-cast-to-itself" rel="nofollow">here</a> 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?Answer1:
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: <a href="http://download.oracle.com/docs/cd/E17904_01/web.1111/e13706/classloading.htm#WLPRG495" rel="nofollow">http://download.oracle.com/docs/cd/E17904_01/web.1111/e13706/classloading.htm#WLPRG495</a>
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.Answer2:
See this <a href="https://stackoverflow.com/questions/5483420/strange-classcastexception-hibernate-3-5-glassfish" rel="nofollow">similiar question</a> with a proposed workaround.Answer3:
This indeed looks like a classloading problem.
<a href="https://stackoverflow.com/questions/2614989/brainstorming-weird-jpa-problem-possibly-classpath-or-jar-versioning-problem/2615014#2615014" rel="nofollow">This earlier answer of mine</a> 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.