I have a legacy (badly modelled) table structure where tables are aranged as in mapping below. My problem is as follows.
Assuming that you have identified a row in SERVICE_BENEFIT_HIERARCHY and I want to return the children of this row from the same table, then to do this, I select from SERVICE_BENEFIT_HIERARCHY joined with SERVICE_BENEFIT_CATEGORY where SERVICE_BENEFIT_CATEGORY.TYP_IND = "C" and relationship of child is like the first 2 characters of relationship of parent.
I know it's the worst possible design but I didn't design it......
So my question is... Is there a way to map this short of writing HQL in a dao accessor?
Also is there a way to put a HQL mapping in hibernate i.e something like.
Code:
<set name="serviceBenefitHierarchyChildrenSet"
inverse="true"
lazy="true"
cascade="all"
>
<one-to-many class="ServiceBenefitHierarchy">
<hql>
<statement>
SELECT b
FROM ServiceBenefitHierarchy as h
join h.ServiceBenefitCategorySet as s
WHERE h.relationship like ?.substring(0,1) || "%"
AND s.typeInd = ?
AND h.hierarchy = ?
ORDER BY h.relationship
<statement>
<column>RELATIONSHIP<column>
<value type="string">C</value>
<column>HIERARCHY_ID</column>
</hql>
</one-to-many>
</set>
This would be very powerful functionality. Of course java substring would have to be implemented since I don't believe this functionality exists.
If a way to do this doesn't exist. Does anyone out there have an idea what it would take to implement this functionality in hibernate.
Hibernate version: 2.1.6
Mapping documents:Code:
<class name="ServiceBenefitHierarchy" table="SERVICE_BENEFIT_HIERARCHY">
<composite-id class="ServiceBenefitHierarchyPK" name="id">
<key-many-to-one
class="SbhId"
column="HIERARCHY_ID"
name="hierarchy"
/>
<key-many-to-one
class="ServiceBenefitCategory"
column="SERVICE_BENEFIT_CATEGORY_ID"
name="serviceBenefitCategory"
/>
</composite-id>
<property
column="DETAIL_SUMMARY"
length="1"
name="detailSummary"
not-null="true"
type="string"
/>
<property
column="HIGH_SUMMARY"
length="1"
name="highSummary"
not-null="true"
type="string"
/>
<property
column="RELATIONSHIP"
length="3"
name="relationship"
not-null="true"
type="string"
/>
<set
inverse="true"
lazy="true"
name="serviceBenefitCategorySet"
>
<key column="SERVICE_BENEFIT_CATEGORY_ID" />
<one-to-many class="ServiceBenefitHierarchy" />
</set>
</class>
<class name="SbhId" table="SBH_ID">
<id
column="HIERARCHY_ID"
name="id"
type="integer"
>
<generator class="sequence">
<param name="sequence">HIERARCHY_ID_SEQ</param>
</generator>
</id>
<property
column="SERVICE_BENEFIT_HIER_EFF_DATE"
length="7"
name="serviceBenefitHierEffDate"
not-null="true"
type="date"
/>
<many-to-one
class="ProductType"
name="productType"
not-null="true"
>
<column name="PRODUCT_TYPE_ID" />
</many-to-one>
<set
inverse="true"
lazy="true"
name="serviceBenefitHierarchySet"
>
<key column="HIERARCHY_ID" />
<one-to-many class="ServiceBenefitHierarchy" />
</set>
</class>
<class name="ServiceBenefitCategory" table="SERVICE_BENEFIT_CATEGORY">
<id
column="SERVICE_BENEFIT_CATEGORY_ID"
name="id"
type="integer"
>
<generator class="sequence">
<param name="sequence">SERVICE_BENEFIT_CATEGORY_ID_SEQ</param>
</generator>
</id>
<property
column="SERVICE_BENEFIT_CATEGORY_NAME"
length="100"
name="serviceBenefitCategoryName"
not-null="false"
type="string"
/>
<property
column="TYPE_IND"
length="1"
name="typeInd"
not-null="false"
type="string"
/>
<set
inverse="true"
lazy="true"
name="serviceBenefitHierarchySet"
>
<key column="SERVICE_BENEFIT_CATEGORY_ID" />
<one-to-many class="ServiceBenefitHierarchy" />
</set>
</class>
Name and version of the database you are using: Oracle 8.1.7