Having stared at the documentation longer and thought about what I need to do, it appears that using a native sql named query and a <loader> for a collection will do what I'm trying to do. Unfortunately, <loader> for collections is on the TODO list as far as documentation goes. Has anyone else figured this out, or could the dev team clue me in?
Here's my new scenario:
I have two classes, StudentRecord and Major. StudentRecord has the 'many' end of a many-to-one association with Major, a bag named 'declaredMajors'. I need to load that collection with some native SQL, because of the way it's stored. Here's the mapping for Major:
Code:
<hibernate-mapping package="edu.taylor.domain.services.registrar">
<class name="Major" table="STVMAJR"
where="STVMAJR_VALID_MAJOR_IND = 'Y'">
<id name="code" column="STVMAJR_CODE"/>
<property name="description" column="STVMAJR_DESC"/>
</class>
</hibernate-mapping>
And here's the mapping for StudentRecord:
Code:
<hibernate-mapping package="edu.taylor.domain.services.registrar">
<class name="StudentRecord" table = "SHRLGPA"
where="shrlgpa_levl_code = 'UG' and shrlgpa_gpa_type_ind = 'I'">
<id name="pidm" column="shrlgpa_pidm"/> <!-- no generator, we always assign this -->
<property name="institutionalHours" column="SHRLGPA_HOURS_EARNED"/>
<property name="cumGpa" column="SHRLGPA_GPA"/>
<bag name="declaredMajors" lazy="true" inverse="true">
<key column="SORLFOS_PIDM"/>
<one-to-many class="Major"/>
<loader query-ref="loadDeclaredMajors"/>
</bag>
</class>
<sql-query name="loadDeclaredMajors">
<load-collection alias="dm" role="StudentRecord.declaredMajors"/>
select
s1.SORLFOS_MAJR_CODE as {dm.code},
maj.STVMAJR_DESC as {dm.description}
from
SORLFOS s1, STVMAJR maj
where s1.SORLFOS_PIDM=:id and
s1.SORLFOS_CACT_CODE = 'ACTIVE'
and (s1.SORLFOS_CSTS_CODE = 'INPROGRESS' OR SORLFOS_CSTS_CODE = 'COMPLETED')
and (s1.SORLFOS_LFST_CODE = 'MAJOR')
and s1.SORLFOS_MAJR_CODE = maj.STVMAJR_CODE
and s1.sorlfos_term_code = (
select max(s2.SORLFOS_TERM_CODE)
from sorlfos s2
where s1.sorlfos_pidm = s2.sorlfos_pidm and s1.sorlfos_majr_code = s2.sorlfos_majr_code)
group by s1.SORLFOS_PIDM, s1.SORLFOS_MAJR_CODE, maj.STVMAJR_DESC </sql-query>
</hibernate-mapping>
And here's the query that Hibernate runs:
Code:
select
s1.SORLFOS_MAJR_CODE as dm.SORLFOS_PIDM as SORLFOS3_0__, dm.STVMAJR_CODE as STVMAJR1_0__, STVMAJR1_0_,
maj.STVMAJR_DESC as dm.SORLFOS_PIDM as SORLFOS3_0__, dm.STVMAJR_CODE as STVMAJR1_0__, STVMAJR2_3_0_
from
SORLFOS s1, STVMAJR maj
where s1.SORLFOS_PIDM=? and
s1.SORLFOS_CACT_CODE = 'ACTIVE'
and (s1.SORLFOS_CSTS_CODE = 'INPROGRESS' OR SORLFOS_CSTS_CODE = 'COMPLETED')
and (s1.SORLFOS_LFST_CODE = 'MAJOR')
and s1.SORLFOS_MAJR_CODE = maj.STVMAJR_CODE
and s1.sorlfos_term_code = (
select max(s2.SORLFOS_TERM_CODE)
from sorlfos s2
where s1.sorlfos_pidm = s2.sorlfos_pidm and s1.sorlfos_majr_code = s2.sorlfos_majr_code)
group by s1.SORLFOS_PIDM, s1.SORLFOS_MAJR_CODE, maj.STVMAJR_DESC
Clearly I'm doing something completely wrong - but since the documentation is incomplete, I'm not sure where to go from here.