JPA 本机查询连接返回对象,但取消引用会引发类转换异常

发布于 2024-10-09 06:16:45 字数 1991 浏览 3 评论 0原文

我使用 JPQL Native 查询来连接表,查询结果存储在 List 中。

public String getJoinJpqlNativeQuery() {



            String final SQL_JOIN = 
               "SELECT v1.bitbit, v1.numnum, v1.someTime, t1.username, 
                t1.anotherNum FROM MasatosanTest t1 
                JOIN MasatoView v1 ON v1.username = t1.username;"

            System.out.println("get join jpql native query is being called 
        ============================");

            EntityManager em = null;
            List<Object[]> out = null;
            try {
                em = EmProvider.getDefaultManager();
                Query query = em.createNativeQuery(SQL_JOIN);
                out = query.getResultList();

                System.out.println("return object ==========>" + out);

                System.out.println(out.get(0));

                String one = out.get(0).toString(); //LINE 77 where ClassCastException
                System.out.println(one);
            }
            catch(Exception e) {
            }
            finally {
                if(em != null) { em.close; }
            }
}

问题是

System.out.println("return object ==========>" + out); 输出:

return object ==========>
[[true, 0, 2010-12-21 15:32:53.0, masatosan, 0.020], 
[false, 0, 2010-12-21 15:32:53.0, koga, 0.213]]

System.out.println(out.get (0)) 输出:

[true, 0, 2010-12-21 15:32:53.0, masatosan, 0.020]

所以我假设我可以分配 out.get(0) 的返回值,它应该是 String:

String one = out.get(0).toString();

但我得到了奇怪的 ClassCastException。

java.lang.ClassCastException: java.util.Vector cannot be cast to 
[Ljava.lang.Object;
        at local.test.jaxrs.MasatosanTestResource.getJoinJpqlNativeQuery
(MasatosanTestResource.java:77)

那么究竟发生了什么?甚至 Object[] foo = out.get(0); 也会抛出 ClassCastException :(

I'm using JPQL Native query to join table and query result is stored in List<Object[]>.

public String getJoinJpqlNativeQuery() {



            String final SQL_JOIN = 
               "SELECT v1.bitbit, v1.numnum, v1.someTime, t1.username, 
                t1.anotherNum FROM MasatosanTest t1 
                JOIN MasatoView v1 ON v1.username = t1.username;"

            System.out.println("get join jpql native query is being called 
        ============================");

            EntityManager em = null;
            List<Object[]> out = null;
            try {
                em = EmProvider.getDefaultManager();
                Query query = em.createNativeQuery(SQL_JOIN);
                out = query.getResultList();

                System.out.println("return object ==========>" + out);

                System.out.println(out.get(0));

                String one = out.get(0).toString(); //LINE 77 where ClassCastException
                System.out.println(one);
            }
            catch(Exception e) {
            }
            finally {
                if(em != null) { em.close; }
            }
}

The problem is

System.out.println("return object ==========>" + out); outputs:

return object ==========>
[[true, 0, 2010-12-21 15:32:53.0, masatosan, 0.020], 
[false, 0, 2010-12-21 15:32:53.0, koga, 0.213]]

System.out.println(out.get(0)) outputs:

[true, 0, 2010-12-21 15:32:53.0, masatosan, 0.020]

So I assumed that I can assign return value of out.get(0) which should be String:

String one = out.get(0).toString();

But I get weird ClassCastException.

java.lang.ClassCastException: java.util.Vector cannot be cast to 
[Ljava.lang.Object;
        at local.test.jaxrs.MasatosanTestResource.getJoinJpqlNativeQuery
(MasatosanTestResource.java:77)

So what's really going on? Even Object[] foo = out.get(0); would throw an ClassCastException :(

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

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

发布评论

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

评论(3

空城旧梦 2024-10-16 06:16:45

SELECT 子句查询多个列或实体,结果聚合在 getResultList( ) 返回的 java.util.List 中的对象数组 (Object[]) 中。

 //---

    Query query = manager.createQuery("SELECT v1.bitbit, v1.numnum, v1.someTime, t1.username, t1.anotherNum FROM MasatosanTest t1 JOIN MasatoView v1 ON v1.username = t1.username;");

    List results = query.getResultList( ); // Fetches list containing arrays of object
    Iterator it = results.iterator( );

    while (it.hasNext( )) {

       Object[] result = (Object[])it.next(); // Iterating through array object 

       Boolean first = (Boolean) result[0]; // Fetching the field from array

       /* Likewise for all the fields, casting accordingly to the sequence in SELECT query*/

    }

    //---

编辑:< /强>
为了避免显式转换,您可以使用构造函数表达式,使用适当的参数向实体添加构造函数。

SELECT new org.somepackage.XEntity(x.a, x.b) FROM XEntity x

The SELECT clause queries more than one column or entity, the results are aggregated in an object array (Object[]) in the java.util.List returned by getResultList( ).

 //---

    Query query = manager.createQuery("SELECT v1.bitbit, v1.numnum, v1.someTime, t1.username, t1.anotherNum FROM MasatosanTest t1 JOIN MasatoView v1 ON v1.username = t1.username;");

    List results = query.getResultList( ); // Fetches list containing arrays of object
    Iterator it = results.iterator( );

    while (it.hasNext( )) {

       Object[] result = (Object[])it.next(); // Iterating through array object 

       Boolean first = (Boolean) result[0]; // Fetching the field from array

       /* Likewise for all the fields, casting accordingly to the sequence in SELECT query*/

    }

    //---

Edit :
To avoid casting explicitly, you can go for constructor expression, adding a constructor to the entity with appropriate arguments.

SELECT new org.somepackage.XEntity(x.a, x.b) FROM XEntity x
撩人痒 2024-10-16 06:16:45

我不熟悉 JPQL Native 查询,但您只需使用以下命令进行调试:

Object o = out.get(0);
System.out.println(o.getClass());

然后从那里开始工作。如果它是一个向量,则迭代并查找向量中的内容。

I'm not familiar with JPQL Native query, but you simply debug with:

Object o = out.get(0);
System.out.println(o.getClass());

Then work from there. If it's a vector, iterate through and find what's in the vector.

终难遇 2024-10-16 06:16:45

您可能想要这样做:

Query query = em.createNativeQuery(SQL_JOIN, org.somepackage.XEntity.class);

这样您的输出将被转换为正确的类型。

You may want to do this:

Query query = em.createNativeQuery(SQL_JOIN, org.somepackage.XEntity.class);

So your out will be cast to correct type.

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