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!