如何在使用本机SQL查询时指定数据类型?

时间:2020-12-28 07:52:05

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.

我正在使用hibernate。我编写了本机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列在表中是CHAR(10)但我想要String类型。如何指定数据类型并获取所有列?

2 个解决方案

#1


6  

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()方法,其列名和数据类型。

#2


0  

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.

根据实际列名称及其数据类型更新列名称和数据类型。

#1


6  

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()方法,其列名和数据类型。

#2


0  

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.

根据实际列名称及其数据类型更新列名称和数据类型。