-->
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.  [ 10 posts ] 
Author Message
 Post subject: List vs Bag
PostPosted: Mon Dec 13, 2004 5:27 pm 
Beginner
Beginner

Joined: Thu Oct 14, 2004 10:53 pm
Posts: 45
Hi could someone help explain this.

In hibernate what is the difference between List and Bag? When is it preferable to use one over the other, and which is the one to go for? At the moment I use the bag element, but in my Java code I'm using the ArrayList collection class.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 13, 2004 5:53 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
search the wiki,
bags are usefull when you add an element to an uninitialised big collection, it avoids the collection to be loaded.
All this is related to Set and List contracts

also check bidirectionnal specs

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 13, 2004 6:01 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Java actually has a Bag, but its called Collection.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 13, 2004 8:41 pm 
Beginner
Beginner

Joined: Thu Oct 14, 2004 10:53 pm
Posts: 45
[quote="anthony"]search the wiki,
bags are usefull when you add an element to an uninitialised big collection, it avoids the collection to be loaded.
All this is related to Set and List contracts

also check bidirectionnal specs[/quote]

thanks. i guess i was just looking for as to which one i should be using most of the time. If a List offers more benefits than a Bag, then obviously I'll use a list but I'm just curious as to which scenarios.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 13, 2004 10:30 pm 
Regular
Regular

Joined: Fri Nov 12, 2004 12:07 am
Posts: 57
Location: Beijing,China
I guess when you only get objects insert,bag is a little better than set or map,otherwise you shouldn't use bag in other cases else.Bacause bag will clear all objects contained by itself and reload all objects when it get objects delete or insert.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 14, 2004 3:42 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Please read the documentation, there are many hints when to use what collection.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 14, 2004 8:39 pm 
Beginner
Beginner

Joined: Thu Oct 14, 2004 10:53 pm
Posts: 45
When using a list element, I noticed that my index column in my BlogItem table "indexCol" doesn't get populated with the index of the element in the list, it still remains as null.

This is my mapping file containing the list part.
<list name="items" inverse="true" lazy="true" cascade="all">
<key column="BlogIDFK"/>
<index column="indexCol"/>
<one-to-many class="eg.BlogItem"/>
</list>

However when I take away the inverse=true part of the list tag, the index of the object in the list is written to the indexCol column. Can someone explain why it happens like that?
<list name="items" lazy="true" cascade="all">
<key column="BlogIDFK"/>
<index column="indexCol"/>
<one-to-many class="eg.BlogItem"/>
</list>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 14, 2004 11:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
http://www.hibernate.org/193.html


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 15, 2004 5:03 pm 
Beginner
Beginner

Joined: Thu Oct 14, 2004 10:53 pm
Posts: 45
Thanks for the link. The information there solved the problem. However I'm just wondering which approach is more correct since they both do the same thing and produce the same result (setting inverse to false, or setting inverse to true and handling the index of the collection manually by having an index property in your class). Why set inverse to true when you have to manually handle the index?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 10, 2005 6:13 pm 
Newbie

Joined: Tue Dec 09, 2003 2:27 am
Posts: 7
Location: Sydney -
michael wrote:


It would appear that it is not possible then to have a
list with inverse="true" for a many-to-many relationship.


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