ClassCastException when casting to the same class
I am not quite following your description of the program flow, but usually when you get ClassCastExceptions you cannot explain you have loaded the class with one classloader then try to cast it to the same class loaded by another classloader. This will not work - they are represented by two different Class objects inside the JVM and the cast will fail.
There is an article about classloading in WebSphere. I cannot say how it applies to your application, but there are a number of possible solutions. I can think of at least:
Change the context class loader manually. Requires that you can actually get a reference to an appropriate class loader, which may not be possible in your case.
Thread.currentThread().setContextClassLoader(...);
Make sure the class is loaded by a class loader higher in the hierarchy.
Serialize and deserialize the object. (Yuck!)
There is probably a more appropriate way for your particular situation though.
java.lang.ClassCastException (Cannot cast class to same class)
Since this approach didnt work. We ended up using MDC to put the keys and AOP @Pointcut and @Around the inject the data into the logger.
ClassCastException from exact same class
It looks like you have 2 ClassLoaders involved. You can check the classloaders with
Customer.class.getClassLoader();
em.createNamedQuery("Customer.findByEmail").setParameter("email", user).getSingleResult().getClass().getClassLoader();
Then you can try to find more information.
ClassCastException when casting to the same class
I am not quite following your description of the program flow, but usually when you get ClassCastExceptions you cannot explain you have loaded the class with one classloader then try to cast it to the same class loaded by another classloader. This will not work - they are represented by two different Class objects inside the JVM and the cast will fail.
There is an article about classloading in WebSphere. I cannot say how it applies to your application, but there are a number of possible solutions. I can think of at least:
Change the context class loader manually. Requires that you can actually get a reference to an appropriate class loader, which may not be possible in your case.
Thread.currentThread().setContextClassLoader(...);
Make sure the class is loaded by a class loader higher in the hierarchy.
Serialize and deserialize the object. (Yuck!)
There is probably a more appropriate way for your particular situation though.
SpringXD: ClassCastException of same class between modules in a stream
See ClassCastException when casting to the same class . This is almost certainly a class loader issue. Try removing spring-social from the modules' class path and add it to xd/lib so it's loaded by the same class loader.
Getting class cast exception where both classes are exactly the same
This happens when two different ClassLoader
objects load classes with the same name. The equality of two classes in Java depends on the fully qualified name and the class loader that loaded it.
So if two independent class loaders load classes from the same location, then objects of those types will not be able to be cast to each others type, even if their classes are called the same.
Related Topics
Variable Column Names Using Prepared Statements
Non Resizable Window Border and Positioning
How to Instantiate an Abstract Class
Why Must Wait() Always Be in Synchronized Block
Connection Pooling Options With Jdbc: Dbcp VS C3P0
Any Tool For Java Object to Object Mapping
How to Throw Checked Exceptions from Inside Java 8 Streams
Hibernate: Hbm2Ddl.Auto=Update in Production
Difference Between ≫≫≫ and ≫≫
When to Use Generic Methods and When to Use Wild-Card
How to Cast a List of Supertypes to a List of Subtypes
What Does Java.Lang.Thread.Interrupt() Do
When Do You Use Varargs in Java
Does Spring @Transactional Attribute Work on a Private Method