简体   繁体   English

如何知道Hibernate中本机SQL查询结果的类型?

[英]How to know the type of a native SQL query result in Hibernate?

I have a native SQL query in Hibernate. 我在Hibernate中有一个本机SQL查询。 I get the query result by : List l = query.list() . 我通过以下方式获得查询结果: List l = query.list()

I know that each element of the list corresponds to a line of the result table. 我知道列表的每个元素都对应于结果表的一行。 But what are the types of those elements ? 但这些元素的类型是什么?

Java tells me they are of type : Object. Java告诉我它们的类型为:Object。 Ok but I want more. 好的,但我想要更多。 Because I want to print the results in the Eclipse console. 因为我想在Eclipse控制台中打印结果。 But for that, I have to know the types, I have to know what this list exactly contains. 但为此,我必须知道类型,我必须知道这个列表究竟包含什么。

Here is the result table of my query I get in SL Developer : 这是我在SL Developer中获得的查询结果表:

结果表如SQL Developer中所示

And know, I want to print all that data in Eclipse console 并且知道,我想在Eclipse控制台中打印所有数据

In Eclipse, I use Query query = session.createSQlQuery("my query"); 在Eclipse中,我使用Query query = session.createSQlQuery("my query"); List l = query.list();` List l = query.list();`

For information, here is the SQL query code : 有关信息,这是SQL查询代码: 我的查询

The Object in the list is an array of Object s (one per column). Object在该列表的阵列Object S(每一个列)。

You have to iterate (that's for each row) then use the array of columns: 你必须迭代(这是每行),然后使用列数组:

final List<Object> l = query.list();
for (final Object row : l) {
    final Object[] columns = (Object[]) row;
    // use columns[0],  columns[1] etc
    System.out.println(columns[0]);
}

If I've got your issue correct, try getClass().getName() methods, which'll give you runtime class of your object. 如果我的问题是正确的,请尝试使用getClass().getName()方法,它将为您提供对象的运行时类。 More info here 更多信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM