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

编程入门 行业动态 更新时间:2024-10-24 18:20:06
本文介绍了使用本机SQL查询时如何指定数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用休眠模式.我已经编写了本机SQL查询,我想指定列之一的数据类型,如下所示.

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.

sqlQuery.addScalar("NAME", STRING);

我要查询5列,而ID是其中的一列.但是,如果我使用addScalar,它不会返回所有列,而仅返回NAME.我将列类型指定为NAME列的原因是表中的CHAR(10),但是我想要的是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?

推荐答案

为避免使用ResultSetMetadata的开销,或者只是为了更明确地返回结果,可以使用addScalar():

To avoid the overhead of using ResultSetMetadata, or simply to be more explicit in what is returned, one can use addScalar():

sess.createSQLQuery("SELECT * FROM CATS") .addScalar("ID", Hibernate.LONG) .addScalar("NAME", Hibernate.STRING) .addScalar("BIRTHDATE", Hibernate.DATE)

此查询指定:

the SQL query string the columns and types to return

这将返回Object数组,但现在将不使用ResultSetMetadata,而是从基础结果集中分别显式获取ID,NAME和BIRTHDATE列,分别为Long,String和Short.这也意味着即使查询使用*,也只会返回这三列,并且返回的列可能多于列出的三列.

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.

参考: docs.jboss/hibernate /orm/3.3/reference/zh-CN/html/querysql.html#d0e13646

因此,在您的情况下,再添加4个addScalar()方法,其列名和数据类型.

So, in your case add 4 more addScalar() method, with its column name and Data type.

更多推荐

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

本文发布于:2023-10-28 19:47:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537601.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:本机   数据类型   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!