如何从sql查询中跳过元组结果中的索引 [英] how to skip an index in a tuple result from a sql query

查看:139
本文介绍了如何从sql查询中跳过元组结果中的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不幸的是,我需要为我的项目的一部分使用'native'sql查询来从未映射表中返回元组。

I unfortunately need to use 'native' sql queries for a portion of my project to return tuples from unmapped tables.

我有一堆查询每行返回相同数量的值,然后进入处理方法将其转换为对象。

I have a bunch of queries which are supposed to return the same number of values per row and then go into a processing method to turn them into objects.

例如:

List<Object[]> list = ...;
List<MyBean> beans = ...;

SQLQuery qry1 = session.createSQLQuery(...);
SQLQuery qry2 = session.createSQLQuery(...);
SQLQuery qry3 = session.createSQLQuery(...);

list.addAll(qry1.list());
list.addAll(qry2.list());
list.addAll(qry3.list());

for(Object[] tuple : list)
{
    MyBean bean = new MyBean();
    bean.setSomething0(tuple[0]);
    bean.setSomething1(tuple[1]);
    bean.setSomething2(tuple[2]);
    bean.setSomething3(tuple[3]);
    beans.add(bean);
}

现在,我的问题是 qry3 没有相关的列来填充元组第二个索引处的值,但是填充第三个索引。我需要修改它的查询,以便用 null 以某种方式填充元组。

now, my problem is that qry3 does not have a relevent column to populate the value at the 2nd index of the tuple, but does populate the 3rd index. I need to modify it's query so that it populates the tuple with null or "" somehow.

我试过select a,b,null,dselect a,b, '',d但是这两个都会导致异常:

I've tried "select a, b, null, d" and "select a, b, '', d" however both of these cause an exception:

org.hibernate.MappingException: No Dialect mapping for JDBC type: 1111
    at org.hibernate.dialect.TypeNames.get(TypeNames.java:56)
    at org.hibernate.dialect.TypeNames.get(TypeNames.java:81)
    at org.hibernate.dialect.Dialect.getHibernateTypeName(Dialect.java:370)
    at org.hibernate.loader.custom.CustomLoader$Metadata.getHibernateType(CustomLoader.java:559)
    at org.hibernate.loader.custom.CustomLoader$ScalarResultColumnProcessor.performDiscovery(CustomLoader.java:485)
    at org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:501)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1796)
    at org.hibernate.loader.Loader.doQuery(Loader.java:674)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
    at org.hibernate.loader.Loader.doList(Loader.java:2213)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
    at org.hibernate.loader.Loader.list(Loader.java:2099)
    at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
    at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
    at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
    at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)

那么我该如何告诉我的查询在这种情况下跳过一个索引?

so how do i tell my query to skip an index in this case?

推荐答案

您可以尝试将其转换为预期的类型:

You can try to cast it to the expected type:

select a, b, cast(null as varchar), d

这篇关于如何从sql查询中跳过元组结果中的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆