• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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."
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic