-->
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: can't cast first element returned by native SQL query
PostPosted: Mon Aug 22, 2011 1:06 pm 
Beginner
Beginner

Joined: Tue Aug 03, 2010 4:32 pm
Posts: 22
So I have the following snippet
Code:
        java.util.List<Object[]> results = null;
        Object[] result;
        ListIterator<Object[]> mi = null;
        String sqlcmd;
       
       sqlcmd = "SELECT uu.code, " +
        "               uu.rank, " +
        "               uu.name, " +
        "               uu.gender   " +
        "            FROM Employees AS uu ";

        SQLQuery hqlQuery = this.sessB.createSQLQuery(sqlcmd);  // native SQL
        results = hqlQuery.list();

        mi = results.listIterator();
        while( mi.hasNext()) {
            result = mi.next();
            System.out.println( Arrays.toString( result) );
            System.out.println( " raw 1st element:" +  result[0] );
            System.out.println( "cast 1st element:" + (String) result[0] );  // <--- throws CCE here
        }


that doesn't want to cast the first element to a string.

The first println writes out
[B, 4, Steve Martin, M]
instead of the expected
[BX0223, 4, Steve Martin, M]

The 2nd println writes out
raw 1st element: B
instead of the expected
raw 1st element: BX0233

and the 3rd println throws a CastClassException : java.lang.Character error

Any ideas what's (not) going on here? Why can't I cast the first element while the rest seem fine?


TIA,

Still-learning Steve


Top
 Profile  
 
 Post subject: Re: can't cast first element returned by native SQL query
PostPosted: Mon Aug 22, 2011 8:58 pm 
Beginner
Beginner

Joined: Tue Aug 03, 2010 4:32 pm
Posts: 22
This was my solution.

this more-enhanced version lets you place the query results directly into a stone-simple
Java class that is =NOT= managed by Hibernate. Great for generating reports where
you'd like to have a class that corresponds to your query row but don't need to store it in a
database table.

The fields in the POJO exactly match up with the columns in the query.

Assume you have a POJO named RptW
Code:
    public class RptW implements java.io.Serializable 
    {   // report class doesn't need an id 
        private String  code; 
        private Long    genderMale;        // need Long to hold SUM(*) 
        private Long    genderFemale; 
        private Long    tot; 
         
        //constructors 
        public RptW() {} 
        public RptW(String code, Long genderMale, Long genderFemale, Long tot) { 
            this.code = code; 
            this.genderMale = genderMale; 
            this.genderFemale = genderFemale; 
            this.tot = tot; 
        } 
         
        //getters and setters 
        public String getCode() { 
            return code; 
        } 
        public void setCode(String code) { 
            this.code = code; 
        } 
        public Long getGenderMale() { 
            return genderMale; 
        } 
        public void setGenderMale(Long genderMale) { 
            this.genderMale = genderMale; 
        } 
        public Long getGenderFemale() { 
            return genderFemale; 
        } 
        public void setGenderFemale(Long genderFemale) { 
            this.genderFemale = genderFemale; 
        } 
        public Long getTot() { 
            return tot; 
        } 
        public void setTot(Long tot) { 
            this.tot = tot; 
        } 
     
        @Override 
        public String toString() { 
            return "RptW [code=" + code + ", genderMale=" + genderMale 
                    + ", genderFemale=" + genderFemale + ", tot=" + tot + "]"; 
        } 
     
    } 


and in the executable

Code:
            java.util.List<RptW> results = null;   // note the direct creation of RptW objects instead of usual Object[] 
            RptW result; 
            ListIterator<RptW> ui = null; 
            String sqlcmd; 
             
            sqlcmd = "SELECT  vv.code AS code, " + 
                     "   SUM( vv.genderMale) AS genderMale, " + 
                     "   SUM( vv.genderFemale) AS genderFemale, " + 
                     "   SUM( vv.tot) AS tot " + 
                     "   FROM Employees AS vv "  +   
                     "   GROUP BY vv.code ";   
     
            Query hqlQuery = this.sessB.createSQLQuery(sqlcmd) 
                    .addScalar("code",       Hibernate.STRING)   // Hibernate magic here! 
                    .addScalar("genderMale",  Hibernate.LONG) 
                    .addScalar("genderFemale", Hibernate.LONG) 
                    .addScalar("tot",       Hibernate.LONG)         
                    .setResultTransformer(Transformers.aliasToBean(RptW.class));   // more magic here 
            results = hqlQuery.list(); 
       
      //verify it worked 
           System.out.println("Q: number of rows:" + results.size() ); 
            ui = results.listIterator(); 
            while( ui.hasNext()) { 
                result = ui.next(); 
                System.out.println( result.toString()); 
            } 


I think this is a powerful technique, hope others enjoy it!

CASE CLOSED

Still-learning Steve


Top
 Profile  
 
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.