These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]

Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate native SQL returns a String instead of an Object[]
PostPosted: Sun Aug 20, 2017 6:45 pm 

Joined: Sun Nov 29, 2015 4:27 pm
Posts: 8
I have the code:
   public SQLQueriesResult execute(SQLQueries action, ExecutionContext context) throws ActionException {

      Session session = null;
      index_grid = 0;
      Integer count = 0;

      Integer offset = action.getLoadConfig().getOffset();
      Integer limit = offset + action.getLoadConfig().getLimit();
      List<SQLRecord> records = new ArrayList<SQLRecord>();

      try {
         session = sessionFactory.openSession();
         String sql = null;
         if (action.isLiveGrid()) {
            sql = liveGridSQL(action.getSql(), limit, offset);
         } else {
            sql = action.getSql();
         SQLQuery query = session.createSQLQuery(sql);

         List<MetaDataFieldConfigSQL> columns = SQLToolsImpl.getColumnFromSQL(session, sql);
         for (int i = 0; i < columns.size(); i++) {
         Iterator<Object[]> result = query.list().iterator();
            while (result.hasNext()) {
            [b]Object[] type = (Object[]) result.next(); -- this error[/b]            
         for (int i = 0; i < result.size(); i++) {
            SQLRecord sqlRecord = new SQLRecord();
            sqlRecord.addField("INDEX_GRID", ++index_grid);
            for (int j = 0; j < columns.size(); j++) {
                     getFieldValue(columns.get(j).getFieldType(), result.get(j)));
         if (!action.isLiveGrid()) {
            count = records.size();
      } catch (JSQLParserException e) {
      } finally {
      return new SQLQueriesResult(
            new PagingLoadResultBean<SQLRecord>(records, records.size(), action.getLoadConfig().getOffset()),

Why does hibernate return 'String' instead of Object []?

[ERROR] Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object;
[ERROR]    at pl.cba.lukaszbaczek.webmuseum.gui.gxt.widgets.sqlGrid.server.data.SQLQueriesHandler.execute(SQLQueriesHandler.java:74)
[ERROR]    at pl.cba.lukaszbaczek.webmuseum.gui.gxt.widgets.sqlGrid.server.data.SQLQueriesHandler.execute(SQLQueriesHandler.java:26)
[ERROR]    at com.gwtplatform.dispatch.rpc.server.AbstractDispatchImpl.doExecute(AbstractDispatchImpl.java:154)
[ERROR]    ... 43 more

Hibernate Version is 5.1.0.Final

 Post subject: Re: Native SQL not return Object[]
PostPosted: Wed Aug 23, 2017 3:32 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
If you call addScalar(alias) only once, you get a List<T> where T is the inferred type of the single column being selected.

If you call addScalar(alias) multiple times, you get a List<Object[]> instead.

You got the ClassCastException because you are in the first situation.

There are many other issues with your approach:

- It looks like you are using a custom-made pagination query building instead of the Hibernate-based one
- It looks like you want a Tuple result which is simpler than your approach or a ResultTransformer
- Don't use e.printStackTrace() because you are going to write to the System.out stream. Use a Logging framework to pass the exception.
- You don't use explicit transactions, and that's almost always a bad idea.

Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.