Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

when to use set & bag?????????

 
gopal kishan
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

can anybody tell me when to SET and when to use BAG in one-to-many.

thanks in advance


regards
Gopal
 
Peter Laurinec
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
I am not sure whether I understand your question correctly.
Please ask a Meaningful question next time.

You usually use Set in one-to-many association that is inverse to many-to-one associaton between 2 classes.
 
gopal kishan
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In one-to-many relation in hibernate, we are using
<set name="somename" and other attributes.....>

as well as we are also using
<bag name="somename" and other attributes.....>

i want to know in which situation we have to use SET or BAG.

there is no clear definition for these in documnets.

thanks in advance

hope you understand my doubt..
 
Paul Clapham
Sheriff
Posts: 21572
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not all that clear to me either. It looks like a "bag" is a list that isn't guaranteed to be in any particular order. Since it's a list and not a set, it can contain duplicate entries (unlike sets). That's my reading of the sentence.

I also found this in an unrelated page when I did a Google search for "list set bag":

"Sets and bags are thus distinguished from lists in that the order in which the values are specified does not matter for the former, but does matter for the latter, while sets are distinguished from bags and lists in that repetitions of values do not count for the former but do count for the latter."
 
Sunil Dixit
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Set can't contain the duplicate value/object while Bag can.
Consider a scenario where u have 1:N relationship b/w POJOA and POJOB
and POJOA conatins collection (i.e Set)of POJOB.

Case : 1
Now If the values in FK_COLUMN_IN_YYYY_TABLE are unique then use <Set> element.
In the hibernate mapping for POJOA class u have following code
<set name ="XXXX" table ="YYYY">
<key column ="FK_COLUMN_IN_YYYY_TABLE"/>
....
</set>

Case : 2
If the values in FK_COLUMN_IN_YYYY_TABLE are duplicate
then following changes are required

a]In the database add a primary key column PK_COLUMN_IN_YYYY_TABLE
b]use <Bag> or <IdBag>
In the hibernate mapping for POJOA class u have following code
<idbag name ="XXXX" table ="YYYY">
<collection-id column ="PK_COLUMN_IN_YYYY_TABLE">
...
</collection-id>
<key column = "FK_COLUMN_IN_YYYY_TABLE"/>
...
</idbag>

If this answers ur query then kindly let me know buddy

Sunil Dixit
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic