Advertisement

04.11.2008 at 05:46AM PDT, ID: 23314849
[x]
Attachment Details

Lazy loading using named query in Hibernate

Asked by prashanth9 in Java Programming Language, Oracle 9.x

Tags: Opensource, Hibernate, 3.2.6

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.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,
PrashanthStart Free Trial
[+][-]04.11.2008 at 11:20AM PDT, ID: 21336769

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Java Programming Language, Oracle 9.x
Tags: Opensource, Hibernate, 3.2.6
Sign Up Now!
Solution Provided By: prashanth9
Participating Experts: 0
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628