-->
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: Ordering by a composite ID object produces invalid SQL
PostPosted: Fri Oct 14, 2005 8:22 pm 
Newbie

Joined: Fri Oct 14, 2005 7:59 pm
Posts: 2
Location: Seattle, WA
So I'm using Hibernate 3.0.5 with Postgres 8.0.4, and I'm encountering a problem where ordering by a composite object produces invalid SQL. I have the following mapping:


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
    package="com.sourcelabs.tpccOM">

    <class name="NewOrder" table="new_order">

        <composite-id  name="orderId" class="OrderId">
            <key-many-to-one name="district" class="com.sourcelabs.tpccOM.District">
                <column name="no_w_id"/>
                <column name="no_d_id"/>
            </key-many-to-one>
            <key-property name="o_id" column="no_o_id" type="integer"/>
        </composite-id>

        <one-to-one name="order" class="com.sourcelabs.tpccOM.Order"/>
    </class>
</hibernate-mapping>


and I'm creating a query using the following call:

Code:
return getSession().createQuery("from NewOrder as no where no.orderId.district.districtId.d_id=?" +
               " and no.orderId.district.districtId.warehouse=?" +
               " order by no.orderId asc")
               .setInteger(0, districtId)
               .setInteger(1, warehouseId)
               .setFetchSize(2)
               .iterate();


It produces an ORDER BY clause that looks like this:

Code:
order by (neworder0_.no_w_id, neworder0_.no_d_id, neworder0_.no_o_id) asc


The correct SQL would look like this:

Code:
order by neworder0_.no_w_id asc, neworder0_.no_d_id asc, neworder0_.no_o_id asc


So my questions are:

* Is this createQuery syntax considered to be invalid HQL? SHOULD it work?
* Or should Hibernate throw an error here?

(Stating the column names specifically in the order by clause, instead of stating the composite ID, works as expected.)

If this is a bug, I'd really like to work on fixing it. I've started digging into the Hibernate source, but I'm not too sure where to start. By the same token, if Hibernate should throw an exception here, I'd like to work on creating a patch that does that. I've started looking at the Hibernate source and trying to figure out how to do this, but I'd really appreciate input on this from the developer community first.

Also, if this is a bug, any information on how the query parser works and where to start with an eye towards fixing this would be appreciated. I've been looking at the ANTLR grammars and the relevant Java classes, but it's a lot to absorb and some pointers would help.

Thank you very much for your time!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 4:24 am 
Pro
Pro

Joined: Mon Jan 24, 2005 5:39 am
Posts: 216
Location: Germany
Hi,

I have the same problem with hibernate 3.1 rc 1 and db2.

A HQL query like:

Quote:
from x order by id


with a composite-primary-key is resolved with brackets like:

Quote:
select ... from x order by (x.a, x.b, x.c)


DB2 accepts:

Quote:
select ... from x order by x.a, x.b, x.c



The obvious work around is to access the fields of your id class
in HQL like:

Quote:
from x order by id.a, id.b, id.c


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.