-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Lazy loading using named query
PostPosted: Fri Apr 11, 2008 8:38 am 
Newbie

Joined: Thu Apr 10, 2008 3:56 pm
Posts: 2
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


Top
 Profile  
 
 Post subject: Lazy loading with named query
PostPosted: Fri Apr 11, 2008 2:17 pm 
Newbie

Joined: Thu Apr 10, 2008 3:56 pm
Posts: 2
Although I could not tie up a named query, I was able to put in a restriction for the type of addresses I wanted. Here are the changed files (sections bolded), for anyone who might face a similar problem...

---------------------------------------
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 -->
<many-to-one property-ref="custprops" name="customer" class="com.mytest.entity.Customer" update="false" insert="false">
<column name="CUSTOMER_ID" />
</many-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....../>

<properties name="custprops" unique="true" update="false">
<property name="customerId" column="CUSTOMER_ID" insert="false" update="false"/>
</properties>


<!-- bi-directional one-to-many association to Customeraddress -->
<set name="customeraddresses" lazy="true" inverse="false" cascade="all" where="ADDR_TYPE='P'">
<key>
<column name="CUSTOMER_ID" />
</key>

<one-to-many class="com.bmw.dcsnet.service.entity.CustomerAddress"/>
</set>
</class>
</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....../>

</class>
</hibernate-mapping>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.