如何从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,d
和select 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