how to skip an index in a tuple result from a sql query
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.
eg:
List<Object[]> list = ...;
List<MyBean> beans = ...;
SQLQuery qr开发者_运维问答y1 = 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);
}
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.
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
Or, just check the length of the tuple array, and don't try to set a parameter that you don't have...
精彩评论