Hibernate version: 3.2.6.ga
Let me first explain my database layout:
-> There is a table called "TableParent" with a primary key and some columns
-> There is a table called "TableChild" with a primary key, some columns and a foreign key to TableParent, which is unique and not null
-> So there is a one to one-or-null relation between TableParent and TableChild.
My mapping files look like this:
Code:
<hibernate-mapping>
<class name="TableParent" table="tableParent">
...
<one-to-one name="rowChild" class="TableChild" property-ref="rowParent" fetch="join"/>
...
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="TableChild" table="tableChild">
...
<many-to-one name="rowParent" column="parentId" class="RowParent" not-null="true" unique="true"/>
...
</class>
</hibernate-mapping>
I want to create a query which only selects some of the properties on both tables (and returns null for properties on the child table if there is no child table associated with the parent row) (and i don't want to fetch the child rows together with the parent rows in a single db statement), so my first attempt was to try the following:
Code:
SELECT r.propertyA, r.propertyB, child.propertyA FROM TableParent r LEFT JOIN FETCH r.rowChild child
which does not work because hibernate complains that r itself is not contained in the query... so i added r to the select part of the query (although i don't use it) and then the query can be executed. The problem is that running the query is slow and uses a lot of memory, because hibernate has to create object instances for all returned rows and transfer all the data from the db to the appserver for the selected rows instead of just the properties i want to read (the table contains a lot of columns, hence a lot of data per row)... here is the working query:
Code:
SELECT r.propertyA, r.propertyB, child.propertyA, r FROM TableParent r LEFT JOIN FETCH r.rowChild child
My question is: How can i do a left join fetch without selecting r itself? Is there a way to do so?