Java实体的新功能:实体类抱怨被强制转换为自身 [英] New to Java Entities: Entity class complaining of being cast to itself

查看:110
本文介绍了Java实体的新功能:实体类抱怨被强制转换为自身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚接触Java实体,并且在Query.getSingleResult()调用中出现了一个奇怪的问题。这是一个代码段:

  LoaPoliciesConfig policy = new LoaPoliciesConfig(); 
EntityManager em = getEntityManager();
try
{
查询q = em.createNamedQuery( LoaPoliciesConfig.findByName);
q.setParameter( policyName,policyName);
对象ret = q.getSingleResult();
策略=(LoaPoliciesConfig)ret;
}

工作了很长时间以后,这突然开始给我这个错误: / p>

  CRAAC.WebService.DataAccess.LoaPoliciesConfig无法转换为
CRAAC.WebService.DataAccess.LoaPoliciesConfig

起初,在重新启动服务器时,这种情况就消失了,但是最后一次出现似乎就停滞了。 p>

getSingleQuery()返回一个Object,到目前为止,我在网上找到的所有文档似乎都表明这是首选的方法。因此,我的问题本质上是我在此代码中是否做错了任何事情,还是仅仅是服务器在途中某处变得混乱了?

解决方案

怀疑,您在类路径上有2个 LoaPoliciesConfig 类,以便可以从两个不同的类加载器中加载/访问它们。



在这种情况下,来自一个类加载器的类 X 与类<$ c不等价来自第二个类加载器的$ c> X ,您会看到混乱。检查您的类路径,环境中可用的类以及它们的加载位置(通过 getClass() getClassLoader()


I'm fairly new to Java Entities, and I've got a bit of a strange problem casting from a Query.getSingleResult() call. Here's a code snippet:

  LoaPoliciesConfig policy = new LoaPoliciesConfig();
  EntityManager em = getEntityManager();
  try
  {
     Query q = em.createNamedQuery("LoaPoliciesConfig.findByName");
     q.setParameter("policyName", policyName);
     Object ret = q.getSingleResult();
     policy = (LoaPoliciesConfig) ret;
  }

After working fine for ages, this has suddenly started giving me this error:

CRAAC.WebService.DataAccess.LoaPoliciesConfig cannot be cast to
CRAAC.WebService.DataAccess.LoaPoliciesConfig

At first this went away when restarting the server, but the last time it happens it seems to be here to stay.

getSingleQuery() returns an Object, and all the documentation I've found on the web so far seem to indicate this is the preferred way of doing it. So, my question is essentially am I do anything wrong in this code, or is it just the server getting confused somewhere along the way?

解决方案

I suspect that you have 2 LoaPoliciesConfig classes on your classpath, such that they are being loaded/accessed from two different classloaders.

In this situation, class X from one classloader is not equivalent to class X from a second classloader, and you'll get the confusion that you're seeing. Check your classpath, and the classes available in your environment, and where they're loaded from (via getClass() and getClassLoader())

这篇关于Java实体的新功能:实体类抱怨被强制转换为自身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆