JPA native query join returns object but dereference throws class cast exception

Posted by masato-san on Stack Overflow See other posts from Stack Overflow or by masato-san
Published on 2010-12-27T04:44:09Z Indexed on 2010/12/27 4:54 UTC
Read the original article Hit count: 315

Filed under:
|
|
|

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 :(

© Stack Overflow or respective owner

Related posts about java

Related posts about sql