-->
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.  [ 6 posts ] 
Author Message
 Post subject: Ordered collection of joined-subclass resulting in bad SQL
PostPosted: Thu Feb 24, 2005 1:03 am 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Ok,

CapturedDocument subclasses Document. DocumentBatch has a collection of CapturedDocuments.

When we try to order the collection by a property of the superclass (Document), Hibernate generates SQL that looks for the superclass property on the subclass' table (see generated SQL below, it seems to be using the wrong SQL alias). We've managed to get our query working by ordering on the primary key (n_key_doc) instead of the docId. But our work-around is a bit of a hack and it seems to me that this should work.

Any thoughts on what we might have done wrong?

I will try to make a reproducible test case for JIRA if anyone can confirm that this is supposed to work.

We've tried it with both a bag and list mapping, the same SQL (with the wrong alias) is generated.

Hibernate version: 2.1.2

Mapping documents:
Code:
<hibernate-mapping>
    <class
        name="nrm.clas.domain.document.Document"
        table="cl_doc"
        lazy="true"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="n_key_doc"
            type="java.lang.Long"
            unsaved-value="null"
        >
            <generator class="net.sf.hibernate.id.TableHiLoGenerator">
                <param name="table">cl_id_gen</param>
                <param name="column">n_key_next</param>
                <param name="max_lo">128</param>
            </generator>
        </id>

        <property
            name="docId"
            type="java.lang.String"
            update="true"
            insert="true"
            column="t_id_doc"
            not-null="true"
            unique="true"
        >
            <meta attribute="clas.optional">false</meta>
            <meta attribute="clas.minimum">1</meta>
            <meta attribute="clas.maximum">20</meta>
        </property>

...

        <joined-subclass
            name="nrm.clas.domain.document.CapturedDocument"
            table="cl_doc_captured"
            dynamic-update="false"
            dynamic-insert="false"
            lazy="true"
        >
        <key
            column="n_key_doc"
        />

...

        <many-to-one
            name="batch"
            class="nrm.clas.domain.document.DocumentBatch"
            cascade="save-update"
            outer-join="auto"
            update="true"
            insert="true"
            column="n_key_doc_batch"
            not-null="true"
       />

        </joined-subclass>

    </class>

</hibernate-mapping>

<hibernate-mapping>
    <class
        name="nrm.clas.domain.document.DocumentBatch"
        table="cl_doc_batch"
        lazy="true"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="n_key_doc_batch"
            type="java.lang.Long"
            unsaved-value="null"
        >
            <generator class="net.sf.hibernate.id.TableHiLoGenerator">
                <param name="table">cl_id_gen</param>
                <param name="column">n_key_next</param>
                <param name="max_lo">128</param>
            </generator>
        </id>

...

        <bag
            name="documents"
            lazy="true"
            inverse="true"
            cascade="none"
            order-by="t_id_doc"
        >

              <key
                  column="n_key_doc_batch"
              />

              <one-to-many
                  class="nrm.clas.domain.document.CapturedDocument"
              />
        </bag>

    </class>

</hibernate-mapping>



The generated SQL (show_sql=true):
Code:
SELECT     ...
FROM       cl_doc_captured documents0_ inner join cl_doc documents0__1_
ON         documents0_.n_key_doc = documents0__1_.n_key_doc
WHERE      documents0_.n_key_doc_batch = ?
ORDER BY   documents0_.t_id_doc

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 27, 2005 1:09 am 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
So, that's a No?

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 27, 2005 1:28 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This is expected. The order-by gets applied to the *collection* table (the table that has the fk).


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 27, 2005 1:30 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Not only expected, but *correct* :-)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 27, 2005 12:06 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
It's a joined-subclass, so HB *knows* there'll be exactly one matching row in the super-table, it's already doing an inner join of the two tables, so why not allow it to issue the order-by clause on the superclass table?
Figuring out that the property is mapped to the super-table should be easy, given it's stated explicitly in the mapping document, right?

Or at least something might be added to the doco along the lines of "don't try to order collections of joined-subclass by any property mapped to the parent table".

Or are you going to call "exotic use-case" on me again? :-b

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 27, 2005 2:44 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
The stuff in the order-by attributes are *not* property names. It is a SQL fragment.


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