Hibernate version: 3.2.6
Hello all,
I have a mapping where an Order has one Customer and each Customer may have multiple CustomerAddresses.
I want to enable lazy loading of Customer and CustomerAddress but want to apply a named query that
filters the CustomerAddress of a particular type. I am not able to figure out how to tie up a named query
and a lazy load feature. Below are the mapping files for my entities. Could anyone please suggest a way
for me to achieve what I want ?
Mapping documents:
---------------------------------------
Hibernate Mapping file for Order Entity
---------------------------------------
<hibernate-mapping>
<class name="com.mytest.entity.Order" table="ORDER" lazy="false">
<composite-id>
<key-property name="orderNbr" column="ORDER_NBR"></key-property>
<key-property name="orderType" column="ORDER_TYPE"></key-property>
</composite-id>
<!-- all the properties -->
<property....../>
<!-- One to one mapping with the Customer table -->
<one-to-one name="customer" class="com.mytest.entity.Customer" lazy="proxy" fetch="select" cascade="none">
<formula>com.mytest.entity.Customer.getCustomerDetails</formula>
</one-to-one>
</class>
</hibernate-mapping>
-------------------------------------------
Hibernate Mapping file for Customer Entity
-------------------------------------------
<hibernate-mapping>
<class name="com.mytest.entity.Customer" table="CUSTOMER" lazy="false">
<id name="customerId" type="java.lang.String" column="CUSTOMER_ID" >
<generator class="sequence" />
</id>
<!-- all the properties -->
<property....../>
<!-- bi-directional one-to-many association to Customeraddress -->
<set name="customeraddresses" lazy="true" inverse="false" cascade="all" >
<key>
<column name="CUSTOMER_ID" />
</key>
<one-to-many class="com.mytest.entity.CustomerAddress" />
</set>
</class>
<query name="com.mytest.entity.Customer.getCustomerDetails">
SELECT cust, custAddress
FROM com.mytest.entity.Customer cust, com.mytest.entity.CustomerAddress custAddress
WHERE cust.customerId = ?
AND cust.customerId = custAddress.customerId
AND custAddress.addrType = 'P'
</query>
</hibernate-mapping>
-------------------------------------------------
Hibernate Mapping file for CustomerAddress Entity
-------------------------------------------------
<hibernate-mapping>
<class name="com.mytest.entity.CustomerAddress" table="CUSTOMERADDRESS" lazy="false">
<composite-id>
<key-property name="customerAddressId" column="CUSTOMER_ADDRESS_ID" />
<key-property name="customerId" column="CUSTOMER_ID" />
</composite-id>
<!-- all the properties -->
<property....../>
<!-- bi-directional many-to-one association to Customer -->
<many-to-one name="customer" class="com.mytest.entity.Customer" update="false" insert="false" lazy="proxy">
<column name="CUSTOMER_ID" />
</many-to-one>
</class>
</hibernate-mapping>
Many thanks in advance...
Regards,
Prashanth
|