0

每次我尝试从我从 jpa 调用中拉回的实体列表中获取一组实体时,我都会遇到一个强制转换异常。例子...

QuickLaunch[] qLaunchArr = null;
List<QuickLaunch> listQL = null;
try
{
    System.out.println("testing 1..2..3");

    //qLaunchArr
    listQL = emf.createNamedQuery("getQuickLaunch").getResultList();
    Object[] objArr = listQL.toArray();
    //System.out.println(listQL.size());
    qLaunchArr = (QuickLaunch[]) listQL.toArray();       
}
catch (Exception e)
{
    System.out.println("Bull Hockey!!!! I can't believe it's not butter!: "+e.toString());
}

[Ljava.lang.Object; incompatible with [Lcom.upmc.esdm.messaging.entities.QuickLaunch;

那是在我的服务器日志中......(我正在使用 WID)而且我也得到了这个异常......

commonj.connector.runtime.DataHandlerException: CWLAP0507E: The response bean class for java class  method GetAllQuickLaunchComponents cannot be created. Reason java.lang.IllegalArgumentException: argument type mismatch.
4

3 回答 3

1

好吧...我想我找到了答案。受这篇文章的启发...

https://stackoverflow.com/a/8060077/729820

我基本上是这样做的

try
    {
        System.out.println("testing 1..2..3");
        listQL = emf.createNamedQuery("getQuickLaunch").getResultList();
        System.out.println("What is the size of this list: number "+listQL.size());
        qLaunchArr = listQL.toArray(new QuickLaunch[listQL.size()]);         
    }

所有的例外似乎都清楚了。谢谢你们的帮助。

于 2012-04-13T13:33:10.547 回答
1

这与 JPA 无关,而是与 Java 有关,因为它不会将 Object[] 转换为 QuickLaunch[]。不能使用 Object 数组代替 QuickLaunch[] 数组吗?

于 2012-04-13T13:34:58.440 回答
1

您可以尝试TypedQuery在没有显式转换和防止异常的情况下获取实体列表。

TypedQuery<QuickLaunch> listQL = em.createNamedQuery("QuickLaunch.getQuickLaunch", QuickLaunch.class);

List<QuickLaunch> products = listQL .getResultList();

此外,更改了查询名称,以更有意义的方式识别其类或结果类型。

于 2012-04-13T12:48:06.760 回答