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

19,268

Solution 1

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.

Solution 2

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
Share:
19,268
Meow
Author by

Meow

I'm a cat.

Updated on June 27, 2022

Comments

  • Meow
    Meow almost 2 years

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