-->
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.  [ 4 posts ] 
Author Message
 Post subject: Regarding mapping collections and adding a collection item
PostPosted: Thu Mar 23, 2006 11:34 am 
Newbie

Joined: Fri Mar 03, 2006 6:57 pm
Posts: 16
Hibernate version: 2/3
with cascade="all"

In general one-to-many mapping situation such as this: "An Order has Many OrderItems"
I have Order and OrderItem classes mapped such that I can do:

orderObject.getOrderItems(); to retrieve all the OrderItems associated with that Order.

and if I wanna add an OrderItem Object to that Order, then I can do:

orderObject.getOrderItems().add( aNewOrderItem );

where aNewOrderItem is a newly instantiated orderItem object.

So, every time I am adding aNewOrderItem object to the set, Hibernate is going to query first and fetch all the objects in the set, then it's going to add the new object to the set.
This works nicely if my Set of OrderItems of orderObject is fairly small.

But what if the Set of orderItems become very large, in such a case wouldn't this operation be rather slow??

Should I rather just use the OrderItemDAO class seperately to add aNewOrderItem? But this approach requires more coding... what is the right approach here?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 24, 2006 12:44 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If the size of the OrderItems table is too large for your DB server to serve up quickly, then yes, you could remove the relationship and do the work in factory code. However, you are labouring under a misconception:
Olonga wrote:
So, every time I am adding aNewOrderItem object to the set, Hibernate is going to query first and fetch all the objects in the set, then it's going to add the new object to the set.

This isn't correct. Hibernate will load items only if you reference them, unless you've specifically told it to eagerly load the items. In your example, Hibernate won't load any members of the OrderItem set if you're just adding a new item. It knows that "insert into OrderItem ..." will work just fine without loading all those other items.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 24, 2006 12:29 pm 
Newbie

Joined: Fri Mar 03, 2006 6:57 pm
Posts: 16
tenwit wrote:
If the size of the OrderItems table is too large for your DB server to serve up quickly, then yes, you could remove the relationship and do the work in factory code. However, you are labouring under a misconception:
Olonga wrote:
So, every time I am adding aNewOrderItem object to the set, Hibernate is going to query first and fetch all the objects in the set, then it's going to add the new object to the set.

This isn't correct. Hibernate will load items only if you reference them, unless you've specifically told it to eagerly load the items. In your example, Hibernate won't load any members of the OrderItem set if you're just adding a new item. It knows that "insert into OrderItem ..." will work just fine without loading all those other items.


Thanks for the reply. However, I still have some confusion. Because when I executed: orderObject.getOrderItems().add( aNewOrderItem );

with hibernate.show_sql turned on, I saw the query for select query to getOrderItems() as well as for insert. So, from what I understand as long as those collection items are not referenced, they will not be acutally initialized, is this correct?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 26, 2006 5:23 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You can prove to yourself whether or not the set is being fully loaded by turning on parameter logging. In you log4j.parameters, add this line:
Code:
log4j.logger.org.hibernate.type=DEBUG
That will show you all the data that is being loaded from the DB. It is possible that the ids for all the OrderItems are being loaded, but I think that that only happens when you use a query to load the items, as opposed to a colleciton mapping. If you're loading an OrderObject and seeing all its OrderItems being loaded, I'd imagine that you are eagerly fetching the collection.

Actually, now that I think about it a bit more, it is probable that the set would have to be fully loaded in order to add a new item. This is because of the general constract of a set: it cannot contain duplicates. The only way to find out if a set contains an item that you're adding is to load every item in the set and compute its hashCode. Because of this, you may find that you will have to change your mapping to a simple many-to-one association from OrderItem to OrderObject, and use a factory class to query an OrderObject's OrderItems.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.