如何处理JPQL查询返回的对象类型? [英] How to deal with Object type returned by JPQL query?

查看:88
本文介绍了如何处理JPQL查询返回的对象类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用JPQL轻松地做到这一点,该JPQL仅返回一个表中的数据.

I can do this easily with JPQL that only returns data from one table.

SELECT m1 FROM MasatosanTest m1

这意味着返回一种数据类型.因此,我可以将查询结果存储到指定类型的列表中:

That means one data type is returned. So I can just store the query result into the List with type specified:

List<MasatosanTest> mt = query.getResultList();

代码段

private static final String JPQL_TEST = "SELECT m1 FROM MasatosanTest m1;

    @Path("innerJoin")
        @GET
        @Produces("application/json")
        public List<MasatosanTest> getJoinedResult() {
            System.out.println("getJoinedResult called");
            EntityManager em = null;
            List<MasatosanTest> mt = null;

            try {
                em = EmProvider.getDefaultManager();
                Query query = em.createQuery(JPQL_TEST);
                mt = query.getResultList();
            }
            catch(Exception e) {
                System.out.println("MasatosanTestResource.java - getJoinedResult ERROR: " + e);
            }
            finally {
                if(em != null) {
                    em.close();
                }
            }
            return mt;
        }

现在,如果我尝试包含2个表的JPQL....

Now, if I try JPQL that involves 2 tables....

查询

SELECT m1, m2 FROM Masatosan m1, Masatosan2 m2;

List<Masatosan> result = query.getResultList(); 

这不会立即导致错误,但是实际上是返回对象类型,而不是返回诸如MasatosanMasatosan2

This does not cause immediate error however actually Object type is being returned instead of particular type such as Masatosan or Masatosan2

因此,当我进行迭代时,会导致CASTException,

So when I iterate, it cause CASTException,

for(Masatosan item : result) { .... }

应对这种情况的好方法是什么?

What is the good way to deal with this scenario?

更新

如果我系统打印变量"result",它会吐出:

If I system print the variable "result" it spits:

return object ==========>
[[Ljava.lang.Object;@1540f1e, [Ljava.lang.Object;@1ac7e54, [Ljava.lang.Object;@199cd0a, 
[Ljava.lang.Object;@6487d7, [Ljava.lang.Object;@125755, [Ljava.lang.Object;@239ff3, 
[Ljava.lang.Object;@da2335, [Ljava.lang.Object;@13da77b, [Ljava.lang.Object;@bea4e1, 
[Ljava.lang.Object;@3add4b, [Ljava.lang.Object;@968e06, [Ljava.lang.Object;@4642c8, 
[Ljava.lang.Object;@ca81a4, [Ljava.lang.Object;@105510f, [Ljava.lang.Object;@cde78, 
[Ljava.lang.Object;@e1b60e, [Ljava.lang.Object;@776306, [Ljava.lang.Object;@6275c, 
[Ljava.lang.Object;@21035, [Ljava.lang.Object;@1762346, [Ljava.lang.Object;@105ea3d, 
[Ljava.lang.Object;@15564f6, [Ljava.lang.Object;@1577817, [Ljava.lang.Object;@18d30be, 
[Ljava.lang.Object;@7b235c, [Ljava.lang.Object;@4e83d4, [Ljava.lang.Object;@b0f862]

首先,我以为我在数组中,但 [第一个括号内的括号似乎只是String的一部分??

First, I thought i was in array but that [ bracket inside the first one seems to be just a part of String???

要测试:

 for(Object items : result) {
            System.out.println("-------------------" + items);
            System.out.println(items.toString());
            return null;
        }

这将输出:

 -------------------[Ljava.lang.Object;@c723e8
[Ljava.lang.Object;@c723e8

因此,"L"旁边的方括号不代表数组.

So the square bracket next to "L" does not represent array.

这意味着查询结果实际上存储在List<Object>中,该List<Object>由2种类型的对象(即M asatosan ObjectMasatotan2对象)组成

This means the query result is actually store in List<Object> which consists of objects of 2 types (i.e. Masatosan Object and Masatotan2 Object)

和ClassCastException :(我很困惑,它应该是可转换的)

And ClassCastException :( I'm confused, it should be castable)

 int count = 0;
        for(Object items : mt) {
            System.out.println("-------------- " + count + " --------------" + items);
            count ++;
            Masatosan woot = (Masatosan) items;
            System.out.println(woot.getUsername());
        }

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to local.test.entity.Masatosan

推荐答案

可能返回List<Object[]>. ClassCastException的消息将具有确切的类型.如果我正确的话:

That possibly returns a List<Object[]>. The message of the ClassCastException will have the exact type. In case I'm correct:

for (Object[] items : result) {
    Masatosan m1 = (Masatosan) items[0];
    Masatosan m2 = (Masatosan) items[1];
}

引用文档

在SELECT子句中使用多个select_expressions时,查询的结果为Object []类型,并且此结果中的元素对应于其在SELECT子句中指定的顺序.

When multiple select_expressions are used in the SELECT clause, the result of the query is of type Object[], and the elements in this result correspond in order to the order of their specification in the SELECT clause

这篇关于如何处理JPQL查询返回的对象类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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