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