Hi,
I have a entity object "customer" and it have "CustomerOrders" as list of the customer orders.
following is my relation in mapping file. I have 14 customer in my database.It is returns all the customer orders with customer object.
Code:
<set name="CustomerOrders" lazy="false" >
<key column="customerId"/>
<one-to-many class="Orders" />
</set>
it is not a good way to get the data. It is loading ""CustomerOrders" collection for all the customers. It is decreasing the application performance. So i set the "lazy=true". It is loading all the customer objects first and when we hit the "CustomerOrders" then it initializing the orders collection. It is also working fine. in single layer architecture.
My code is to get the data using session is ...
Code:
Dim criteria As ICriteria = Session.GetISession().CreateCriteria(GetType(T)).SetMaxResults(maxResults)
result = criteria.List(Of T)()
My problem is my application have different layers. Nhibernate data fetching is in one layer and UI is in seperate layer. When i expand the "CustomerOrders" for a customer in the "datalayer" it is loading the data and working file(in debug mode). The same data is returned to the UI layer and i expand 2nd customer object to see the "CustomerOrders" , it is showing an error message "could not initialize a collection: ".The collection is initializing when i hit the orders in the data layer but the problem with n-tire architecture.
To reduce this problem i added another attribute "fetch=join" in <set> tag.Now it is returns 19 customer object with some duplicate customer object(5 objects are duplicate objects).But the "CustomerOrders" list is loading with lazy. some customers have 2 orders,some have 3 orders and some have empty orders. Every thing is working fine when i add the "fetch=join" attribute.
I have some doubts on this lazy loading. Please help me.
My queries are ....
Q) How can we get the data in UI layer with lazy loading?
Q) How can i avoid duplicate values if i add "fetch=join" in <set> tag?
Regards,
Kiran