Hello all,
I am trying to code a database access object in Java that will fetch an Order with its sub entities.
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 when a select query is run joining Customer and CustomerAddress.
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 ?
--------------------------
----------
---
Hibernate Mapping file for Order Entity
--------------------------
----------
---
<hibernate-mapping>
<class name="com.mytest.entity.Or
der" table="ORDER" lazy="false">
<composite-id>
<key-property name="orderNbr" column="ORDER_NBR"></key-p
roperty>
<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.C
ustomer" lazy="proxy" fetch="select" cascade="none">
<formula>com.mytest.entity
.Customer.
getCustome
rDetails</
formula>
</one-to-one>
</class>
</hibernate-mapping>
--------------------------
----------
-------
Hibernate Mapping file for Customer Entity
--------------------------
----------
-------
<hibernate-mapping>
<class name="com.mytest.entity.Cu
stomer" 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.C
ustomerAdd
ress" />
</set>
</class>
<query name="com.mytest.entity.Cu
stomer.get
CustomerDe
tails">
SELECT cust, custAddress
FROM com.mytest.entity.Customer
cust, com.mytest.entity.Customer
Address 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.Cu
stomerAddr
ess" table="CUSTOMERADDRESS" lazy="false">
<composite-id>
<key-property name="customerAddressId" column="CUSTOMER_ADDRESS_I
D" />
<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.C
ustomer" update="false" insert="false" lazy="proxy">
<column name="CUSTOMER_ID" />
</many-to-one>
</class>
</hibernate-mapping>
Many thanks in advance...
Regards,
Prashanth
Start Free Trial