JPA 本机查询连接返回对象,但取消引用会引发类转换异常
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
SELECT 子句查询多个列或实体,结果聚合在 getResultList( ) 返回的 java.util.List 中的对象数组 (Object[]) 中。
编辑:< /强>
为了避免显式转换,您可以使用构造函数表达式,使用适当的参数向实体添加构造函数。
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( ).
Edit :
To avoid casting explicitly, you can go for constructor expression, adding a constructor to the entity with appropriate arguments.
我不熟悉 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.
您可能想要这样做:
这样您的输出将被转换为正确的类型。
You may want to do this:
So your out will be cast to correct type.