Hibernate version: 3.1.2
I am trying to eagerly populate a many-to-one reference in a native sql query. I thought I had it figured out when I came across the <return-join> element until I realized that the collection being returned was not a collection of my persistent class, but was actually a collection of Obect[].
So my question becomes when is return-join different that just using multiple return elements? They seem to behave in the same way as far as I can tell (at least in my case).
Secondarily, is there any way to use a native sql query, eagerly fetch foreign key references, and still return a collection of the persistent class being queried?
I have seen other people reference similar issues, and I have also reviewed the unit tests (JoinFetchTest) and confirmed that the behavior I am seeing matches the tests, but I just don't understand why this behaves so differently than a "left join fetch" in HQL.
So, now for the relevent bits of code. Here's the class I'm querying:
@Entity(access=AccessType.FIELD)
@Table(name="Procedures")
public class Procedure extends ValueObject
implements Serializable
{
...Base properties omitted for brevity
@ManyToOne()
@JoinColumn(name="iAssetTypeId")
private AssetType _assetType;
...Getters/Setters omitted too.
}
And the two, seemingly equivilent named queries (both return collections of object[])
<sql-query name="procedureBySiteAndNameReturnJoin">
<return alias="p" class="com.itendant.bo.procedure.Procedure"/>
<return-join alias="a" property="p._assetType"/>
SELECT {p.*}, {a.*}
FROM ProcedureLibrary pl,
ServiceProvider sp,
Procedures p,
AssetType a
WHERE pl.iServiceProviderId = sp.iId
AND p.iProcedureLibraryId = pl.iId
AND p.iAssetTypeId = a.iId
AND sp.iSiteId = :iSiteId
AND UPPER(p.sName) LIKE UPPER(:sNameLike)
</sql-query>
<sql-query name="procedureBySiteAndNameMultiReturn">
<return alias="p" class="com.itendant.bo.procedure.Procedure"/>
<return alias="a" class="com.itendant.bo.assettype.AssetType"/>
SELECT {p.*}, {a.*}
FROM ProcedureLibrary pl,
ServiceProvider sp,
Procedures p,
AssetType a
WHERE pl.iServiceProviderId = sp.iId
AND p.iProcedureLibraryId = pl.iId
AND p.iAssetTypeId = a.iId
AND sp.iSiteId = :iSiteId
AND UPPER(p.sName) LIKE UPPER(:sNameLike)
</sql-query>
Thanks in advance for any help/explanation,
Scott
|