问题描述
我使用 JPQL Native 查询来连接表,查询结果存储在 List
中.
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; }
}
}
问题是
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:
So I assumed that I can assign return value of out.get(0) which should be String:
String one = out.get(0).toString();
但我得到了奇怪的 ClassCastException.
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)
那到底是怎么回事?即使 Object[] foo = out.get(0);
也会抛出 ClassCastException :(
So what's really going on? Even Object[] foo = out.get(0);
would throw an ClassCastException :(
推荐答案
我不熟悉 JPQL Native 查询,但您只需调试:
I'm not familiar with JPQL Native query, but you simply debug with:
对象 o = out.get(0);System.out.println(o.getClass());
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.
这篇关于JPA 本机查询连接返回对象但取消引用引发类转换异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!