Hibernate version: 3.2.4.sp1
Name and version of the database you are using: Oracle 10
I have a ContractTier object loaded normally by hibernate via the following mapping:
Code:
<hibernate-mapping>
<class name="ContractTier" table="NPC_CONTRACT_TIER">
<id name="contractTierId" type="long">
<column name="CONTRACT_TIER_ID" precision="18" scale="0" />
<generator class="sequence" ><param name="sequence">NPC_CONTRACT_TIER_SEQ</param></generator>
</id>
<many-to-one name="npcContract" class="Contract" fetch="select">
<column name="NPC_CONTRACT_ID" precision="18" scale="0" not-null="true" />
</many-to-one>
<property name="description" type="string">
<column name="DESCRIPTION" length="50" />
</property>
</class>
</hibernate-mapping>
I have a ContractDifference domain object which contains a collection of these ContractTier objects. I load the ContractDifference object with a custom sql query. Here is the mapping for ContractDifference:
Code:
<hibernate-mapping>
<class name="ContractDifference" mutable="false">
<id name="id" />
<property name="customerContractId" not-null="true" update="false" insert="false" />
<property name="memberName" not-null="true" update="false" insert="false" />
<property name="supplierName" not-null="true" update="false" insert="false" />
<set name="tierAdditions" inverse="true">
<key column="npc_contract_id" />
<one-to-many class="ContractTier"/>
<loader query-ref="getContractTierAdditions"/>
</set>
<loader query-ref="getContractDifferences"/>
</class>
<sql-query name="getContractDifferences">
<return alias="cd" class="ContractDifference" />
<![CDATA[SELECT npc.npc_contract_id as {cd.id},
npc.customer_contract_id as {cd.customerContractId},
mbr.member_name as {cd.memberName},
sca.member_name as {cd.supplierName}
FROM npc_contract npc
JOIN ec_master_contract_header sca
ON npc.contract_id = sca.contract_id
AND npc.supplier_member_id = sca.member_id
AND sca.community = 'mycommunity'
AND sca.revision_id = 1
JOIN ec_master_member mbr
ON npc.member_id = mbr.member_id
WHERE npc.npc_contract_id=:id]]>
</sql-query>
<sql-query name="getContractTierAdditions">
<load-collection alias="tier" role="ContractDifference.tierAdditions" />
<![CDATA[SELECT tier.contract_tier_id,
tier.npc_contract_id,
tier.description
FROM npc_contract_tier tier
JOIN npc_contract npc
ON npc.npc_contract_id = tier.npc_contract_id
LEFT OUTER JOIN ec_master_contract_detail dtl
ON dtl.community = 'mycommunity'
AND dtl.member_id = npc.supplier_member_id
AND dtl.contract_id = npc.contract_id
AND dtl.revision_id = 1
AND dtl.description = tier.description
WHERE
npc.npc_contract_id = :id AND
dtl.description is null]]>
</sql-query>
</hibernate-mapping>
All of the above works fine.
My issue arises once I add a derived property to the ContractTier object as so:
Code:
<hibernate-mapping>
<class name="ContractTier" table="NPC_CONTRACT_TIER">
<id name="contractTierId" type="long">
<column name="CONTRACT_TIER_ID" precision="18" scale="0" />
<generator class="sequence" ><param name="sequence">NPC_CONTRACT_TIER_SEQ</param></generator>
</id>
<many-to-one name="npcContract" class="Contract" fetch="select">
<column name="NPC_CONTRACT_ID" precision="18" scale="0" not-null="true" />
</many-to-one>
<property name="description" type="string">
<column name="DESCRIPTION" length="50" />
</property>
<property name="itemCount"
type="int"
formula="(select count(*) from npc_price_list p where p.Contract_Tier_id = CONTRACT_TIER_ID )"
/>
</class>
</hibernate-mapping>
Is it possible to modify the sql query getContractTierAdditions to add the itemCount property for ContractTier objects? Maybe I'm battling something that is not possible.
Thanks in advance.