views:

471

answers:

3

Hi,

I am doing a JBoss SEAM project and when I view a form I get this error.

java.lang.ClassCastException:
it.cogitoweb.csi.entity.csiorelav.CsiTipoLav cannot be cast to
it.cogitoweb.csi.entity.csiorelav.CsiTipoLav

Its alway the same JPA class which is related to the form which is shown on the screen, it doesn't make sense to me why is it the same class, it seems impossible.

Regards, Philip

+4  A: 

This is because the class has been loaded by two different classloaders. You cannot cast between them.

You've likely got a duplicate copy of CsiTipoLav in your application, and the two different copies are being loaded at different times from different classloaders. JBoss has a plethora of different classloaders in a hierarchy, and it's easy to get things in a twist.

Make sure you only have one copy of the class.

skaffman
+2  A: 

The object you are trying to cast, is loaded by a different classloader than the one which has loaded the class you are trying to cast into.

Thorbjørn Ravn Andersen
+8  A: 

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.

Joachim Sauer