20654

Java: exception when casting to itself(?)

Question:

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.

Recommend

  • How to dill (pickle) to file?
  • Using mkdir -m -p and chown together correctly
  • Securing an ajax request
  • What is the difference between .Value = “” and .ClearContents?
  • Is the “Async” suffix required in the name of an async ASP.NET MVC 4 action?
  • TabControl caching with code behind layout changes
  • Exporting C++ dll to c# [closed]
  • On step changing check file size and file type Jquery-Steps
  • How to use BouncyCastle lightwigth API to generate cms enveloped data
  • Why can't i see some C++ DLL ( support CLR ) in C# project ?
  • Memory leak when resizing UIImage
  • Passing multiString values to installer through command-line
  • PHP file_get_contents
  • After upgrade to GIT bash 2.8.2, cannot run sqlite3
  • Calling SaveAsync method sends email to attendees for existing appointment?
  • ASP.Net Web Application deployment steps
  • Get the textbox Text using ajax on button click
  • UIViewControllerAnimatedTransitioning: Black screen fragments after rotation change
  • openTSDB REST API is not storing data
  • Use of x`apply` to speed up loops
  • build an API for streaming audio/video from GridFS mongodb
  • Multiple git user in single device
  • JQuery Mobile Ajax Navigation in Single-Page Template
  • content must have a ListView whose id attribute is 'android.R.id.list'
  • How to add html image in to velocity template file to send email?
  • Find angle of point on circle
  • How to merge objects within array based on attribute
  • concise way of flattening multiindex columns
  • Send array to next viewcontroller iOs xcode [duplicate]
  • Codeigniniter insert data through models and controller
  • PHP Permalinks.. how to change?
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?