java 使用本机 SQL 查询时如何指定数据类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12860347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to specify data type when using native SQL query?
提问by user1016403
I am using hibernate. I have written native SQL query and I would like to specify data type of one of the column as below.
我正在使用休眠。我已经编写了本机 SQL 查询,我想指定列之一的数据类型,如下所示。
sqlQuery.addScalar("NAME", STRING);
I am querying 5 columns and ID
is one of the column among them. But if I use addScalar
, it is not returning all the columns and is returning only NAME
. the reason why I am specifying the column type is NAME
column is of CHAR(10)
in table but I want String
type. How can I specify the datatype and get all the columns?
我正在查询 5 列,并且ID
是其中的一列。但是,如果我使用addScalar
,它不会返回所有列,而只会返回NAME
. 我指定列类型的原因是NAME
column is of CHAR(10)
in table 但我想要String
类型。如何指定数据类型并获取所有列?
回答by Vinit Prajapati
To avoid the overhead of using ResultSetMetadata, or simply to be more explicit in what is returned, one can use addScalar():
为了避免使用 ResultSetMetadata 的开销,或者只是为了让返回的内容更加明确,可以使用 addScalar():
sess.createSQLQuery("SELECT * FROM CATS")
.addScalar("ID", Hibernate.LONG)
.addScalar("NAME", Hibernate.STRING)
.addScalar("BIRTHDATE", Hibernate.DATE)
This query specified:
此查询指定:
the SQL query string
the columns and types to return
This will return Object arrays, but now it will not use ResultSetMetadata but will instead explicitly get the ID, NAME and BIRTHDATE column as respectively a Long, String and a Short from the underlying resultset. This also means that only these three columns will be returned, even though the query is using * and could return more than the three listed columns.
这将返回 Object 数组,但现在它不会使用 ResultSetMetadata,而是从底层结果集中分别显式获取 ID、NAME 和 BIRTHDATE 列作为 Long、String 和 Short。这也意味着只会返回这三列,即使查询使用 * 并且可能返回多于列出的三列。
Ref: http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querysql.html#d0e13646
参考:http: //docs.jboss.org/hibernate/orm/3.3/reference/en/html/querysql.html#d0e13646
So, in your case add 4 more addScalar()
method, with its column name and Data type.
因此,在您的情况下,再添加 4 个addScalar()
方法及其列名和数据类型。
回答by Yogendra Singh
This is working as it is supposed to work. Either it can use ResultSetMetaData
and return you all the values returned from the query or it can use provided type inputs and return the columns provided with the types.
这是正常工作的。它可以使用ResultSetMetaData
并返回从查询返回的所有值,或者可以使用提供的类型输入并返回随类型提供的列。
If there is no problem, add the type for remaining FOUR columns as well.
如果没有问题,也为剩余的四列添加类型。
sqlQuery.addScalar("NAME", STRING).
.addScalar("ID", Hibernate.LONG)
.addScalar("COLUMN3", STRING)
.addScalar("COLUMN4", STRING)
.addScalar("COLUMN5", STRING);
Update the column names and data type as per your actual column names and their data types.
根据您的实际列名及其数据类型更新列名和数据类型。