-->
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.  [ 5 posts ] 
Author Message
 Post subject: tuple order (named SQL queries vs. createSQLQuery)
PostPosted: Wed Nov 03, 2004 5:15 pm 
Newbie

Joined: Wed Nov 03, 2004 4:40 pm
Posts: 4
Newbie here so I hope this makes sense.

I have been refactoring some Hibernate SQL queries out of Java code into named SQL queries into my Hibernate mapping file. I noticed when I ran the exact same SQL that returns a tuple of three objects, I was getting the objects in a different order than when I specified the query in the code and explicitly provided the alias and class arrays to the createSQLQuery method. I even changed the order of the return elements in the XML but it made no difference.

I poked around the Hibernate source code and noticed that when the Binder class loads the mapping in the XML for the <sql-query> element and <return> element, the alias and class names are placed into a HashMap so the order they are declared in is lost. When the stored query is used, an array of keys and values are returned from this map so the order would be whatever they are stored in this Map.

It seems that this order may be critical for knowing what order to expect the tuples when they are retuned from the list() method of the query. I was under the assumption (can't seem to find any documentation on this for SQL queries) that the order following the "select" would be the order of the tuples. But it doesn't seem the case for me.

Can anyone confirm this or point me to some documentation I've overlooked? If this is the case, is there some way I can ensure (predict) the order of the tuples returned in the list() from a named query? I've included snippets of the mapping file and code and this is similar to the example in the Hibernate In Action book on page 284-285 except I have three alias/classes in the query.

In my example, when useNamedQuery is false, I get a StudyBean, ReportBean and PatientBean object in tuples[0], tuple[1] and tuple[2] respectively. When useNamedQuery is true, I get a PatientBean, StudyBean and ReportBean, in that order.

TIA

bill


Hibernate version: 2.1.6

Mapping documents:
...snippet...
Code:
    <sql-query name="getStudyListForReferringPhysician"><![CDATA[
        select {study.*}, {report.*}, {patient.*}
         from StudyLevel as study
         left join Reports as report
         on study.StuInsUID = report.StudyUID
         left join PatientLevel as patient
         on study.PatParent = patient.PatID
         where study.RefPhyNam = :referringPhysicianName
         order by study.StuInsUID
         ]]>
         <return alias="study" class="com.compressus.estation.bean.StudyBean"/>
         <return alias="report" class="com.compressus.estation.bean.ReportBean"/>
         <return alias="patient" class="com.compressus.estation.bean.PatientBean"/>
     </sql-query>


Code between sessionFactory.openSession() and session.close():
...snippet...
Code:
        boolean useNamedQuery = false;
   
        Query q;
        if ( useNamedQuery )
        {
            q = hibSession.getNamedQuery( "getStudyListForReferringPhysician" );
        }
        else
        {
            String sql = "select {study.*}, {report.*}, {patient.*}"
                + " from StudyLevel as study"
                + " left join Reports as report"
                + " on study.StuInsUID = report.StudyUID"
                + " left join PatientLevel as patient"
                + " on study.PatParent = patient.PatID"
                + " where study.RefPhyNam = :referringPhysicianName"
                + " order by study.StuInsUID";

            String[] phNames = { "study", "report", "patient" };
            Class[] phClasses = { StudyBean.class, ReportBean.class, PatientBean.class };
            q = hibSession.createSQLQuery(sql, phNames, phClasses);
        }
        q.setString("referringPhysicianName", name);
       
        Iterator itTuples = q.list().iterator();

        while (itTuples.hasNext())
        {
            Object[] tuple = (Object[]) itTuples.next();
            log.debug( "tuple.length=" + tuple.length );
            log.debug( "tuple[0]=" + tuple[0] );
            log.debug( "tuple[1]=" + tuple[1] );
            log.debug( "tuple[2]=" + tuple[2] );

            ...

        }



Name and version of the database you are using: mySQL v4.0.20a


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 03, 2004 6:10 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
this sounds like a bug.

please submit it to JIRA

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 03, 2004 6:16 pm 
Newbie

Joined: Wed Nov 03, 2004 4:40 pm
Posts: 4
Thanks, max, I will.

BTW...what is the correct behavior? The order after the "select" or the order that the aliases are defined?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 03, 2004 6:19 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
IMHO the order that the aliases are defined - the other solution would require us parsing the sql + it could not work since you can return the columns in arbitrary order etc.

Patches very welcome (including a unit test ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 03, 2004 6:43 pm 
Newbie

Joined: Wed Nov 03, 2004 4:40 pm
Posts: 4
Added issue (key: HB-1295).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.