-->
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: Insert into very large Set
PostPosted: Mon Nov 13, 2006 5:39 pm 
Newbie

Joined: Mon Nov 13, 2006 4:45 pm
Posts: 5
Hello,

Is there a way to insert an object into a Set without loading the elements of that Set?

I would like to do:
folder.getDocuments().add(document);

However, this requires the entire Set of Documents to be loaded, so that Set.add() may return false if the Document already exists in the Set.

Loading the Set into memory is impractical, because it may contain more than 100,000 items.

Is there some mechanism that I can invoke through the mapping files, HQL, or otherwise to prevent the entire Set from being loaded, just so that I can insert an element into it?

I do not mind if the solution requires that I manually check the database for duplicate instances before inserting the Document. Where I am stumped is how to insert the relationship between Folders and Documents into the database. (It is a uni-directional many-to-many relationship.)

I vaguely remember that the Hibernate Reference Documentation addressed this issue in a previous version, but I cannot find any mention of it in the current 3.2 documentation.

Thank you!

Adam


Top
 Profile  
 
 Post subject: Re: Insert into very large Set
PostPosted: Mon Nov 13, 2006 5:48 pm 
Newbie

Joined: Thu Dec 01, 2005 11:34 am
Posts: 19
atalaat wrote:
Hello,

Is there a way to insert an object into a Set without loading the elements of that Set?

I would like to do:
folder.getDocuments().add(document);

However, this requires the entire Set of Documents to be loaded, so that Set.add() may return false if the Document already exists in the Set.

Loading the Set into memory is impractical, because it may contain more than 100,000 items.

Is there some mechanism that I can invoke through the mapping files, HQL, or otherwise to prevent the entire Set from being loaded, just so that I can insert an element into it?

I do not mind if the solution requires that I manually check the database for duplicate instances before inserting the Document. Where I am stumped is how to insert the relationship between Folders and Documents into the database. (It is a uni-directional many-to-many relationship.)

I vaguely remember that the Hibernate Reference Documentation addressed this issue in a previous version, but I cannot find any mention of it in the current 3.2 documentation.

Thank you!

Adam


I believe what you're looking for is a Bag. From the docs:

Just before you ditch bags forever, there is a particular case in which bags (and also lists) are much more performant than sets. For a collection with inverse="true" (the standard bidirectional one-to-many relationship idiom, for example) we can add elements to a bag or list without needing to initialize (fetch) the bag elements! This is because Collection.add() or Collection.addAll() must always return true for a bag or List (unlike a Set). This can make the following common code much faster.

I hope this helps!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 13, 2006 6:15 pm 
Newbie

Joined: Mon Nov 13, 2006 4:45 pm
Posts: 5
Thanks for the quick reply.

Unfortunately, bags have the following limitation which is a deal-breaker:

"Bags are the worst case. Since a bag permits duplicate element values and has no index column, no primary key may be defined. Hibernate has no way of distinguishing between duplicate rows. Hibernate resolves this problem by completely removing (in a single DELETE) and recreating the collection whenever it changes. This might be very inefficient. "

There is the idbag element to consider.

Do you know if an idbag can:
1) Insert without loading the entire bag first?
2) Update/delete without purging and recreating the entire bag first?

"As you can see, an <idbag> has a synthetic id generator, just like an entity class! A different surrogate key is assigned to each collection row. Hibernate does not provide any mechanism to discover the surrogate key value of a particular row, however."

"Note that the update performance of an <idbag> is much better than a regular <bag>! Hibernate can locate individual rows efficiently and update or delete them individually, just like a list, map or set."

Thank you for the advice.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 13, 2006 6:54 pm 
Beginner
Beginner

Joined: Mon Sep 27, 2004 4:28 pm
Posts: 44
Well, if you define the relationship bidirectionally (such that Documents know about their Folders).. then I'd imagine you could perform the insert or update without forcing hibernate to load the set.


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.