获取类转换异常,其中两个类完全相同

发布于 2024-08-24 10:23:41 字数 266 浏览 5 评论 0原文

我正在做一个 JBoss SEAM 项目,当我查看表单时,我收到此错误。

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

它总是与屏幕上显示的表单相关的相同 JPA 类,这对我来说没有意义为什么它是同一个类,这似乎不可能。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

花落人断肠 2024-08-31 10:23:41

当两个不同的 ClassLoader 对象加载具有相同名称的类时,就会发生这种情况。 Java 中两个类的相等性取决于完全限定名称加载它的类加载器。

因此,如果两个独立的类加载器从同一位置加载类,那么这些类型的对象将无法转换为彼此的类型,即使它们的类被调用相同。

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.

懒猫 2024-08-31 10:23:41

正如 Joachim 之前所解释的,当两个类加载器加载同名的类时,通常会发生 java.lang.ClassCastException。然而,我遇到过另一种可能发生这种情况的情况。

某些自动重新加载已修改的类的 IDE 可能会发生这种情况。在这种情况下,内存中可能会保留该类的旧版本,从而导致 ClassCastException。

以下是解决此问题的几种方法:

  1. 如果您正在编写自定义类加载器,则在加载类时请确保基本/默认类加载器尚未加载该类的实例。

  2. 使正在加载的类成为已由默认类加载器加载的类的子类。

  3. 使正在加载的类实现已由默认类加载器加载的接口。

更多信息请点击这里 - http://www.jspwiki.org/wiki/A2AClassCastException

As Joachim explained earlier, java.lang.ClassCastException typically occurs when two classloaders load the classes with the same name. However, i have come across another situation when this could occur.

This could occur with some IDE's that automatically reloads classes that have been modified. In such cases there might be older versions of the class retained in memory causing ClassCastException.

Here are a few ways you could resolve this issue :

  1. If you are writing a custom class loader, while loading a class make sure that the base/default class loader does not already have an instance of that class loaded.

  2. Make the class being loaded a sub-class of the class that is already loaded by the default class loader.

  3. Make the class being loaded implement an interface that is already loaded by the default class loader.

More info here - http://www.jspwiki.org/wiki/A2AClassCastException

时光磨忆 2024-08-31 10:23:41

这是因为该类已由两个不同的类加载器加载。你不能在它们之间施放。

您的应用程序中可能有 CsiTipoLav 的重复副本,并且这两个不同的副本在不同时间从不同的类加载器加载。 JBoss 在层次结构中具有大量不同的类加载器,并且很容易使事情变得混乱。

确保您只有一份该课程的副本。

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.

所谓喜欢 2024-08-31 10:23:41

您尝试转换的对象是由与加载您尝试转换的类不同的类加载器加载的。

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.

你的往事 2024-08-31 10:23:41

就我而言,我有两个不同的 *.ear,并且想要从另一个加载一个类。
所以我必须隔离类加载器。我使用了以下描述:

http://www.thorgull.be/wiki/index.html php?title=ClassLoader_isolation_in_JBOSS

它对我有用。

In my case i had two different *.ear and wanted to load a class from the other.
So i had to isolate the classloader. I used this description:

http://www.thorgull.be/wiki/index.php?title=ClassLoader_isolation_in_JBOSS

It worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文