-->
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: difference between Bag and List
PostPosted: Mon Dec 08, 2003 1:26 am 
Newbie

Joined: Mon Dec 08, 2003 1:09 am
Posts: 4
I have 2 tables with bi-directional one-to-many relationship.
When I retrieve the data from the parent table, I get the desired parent and child collection(List) data
along with null values. Why is it returning null data.

The problem was solved when I used Bag instead of List in my xml mapping.

What is the difference between Bag and List?

When is it desired to use Bag and when List?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 3:19 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
A bag is an unordered group of data, while the list is ordered, eg, has a index. If the index is not in sequence (or start at 0) then there will be gaps in the data structure hence the nulls.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 6:07 am 
Newbie

Joined: Mon Dec 08, 2003 1:09 am
Posts: 4
Hi,
Thanks for the response.
However my problem persists.

Let me explain whts happenning

For example I have 10 rows in my parent table and 100 rows in my child table(10 child records for each parent) .The table primary key starts from 1 in both parent and child tables.

I try to retrieve 1 row from my parent for example 5th row...
The number of parent records tht i get is 1 (as expected)
but the number of child records is 61, where record 1 - 50 is null and 51 thru 60 gives the expected result.

Where am I going wrong.

The same pgm executed with bag gives me exactly 1 parent and 10 child records(as expected).


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 8:00 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Please post the simplified
    1) HibernateMapping file(s)
    2) DB Schema including script to populate DB
    3) Java code
    4)Exception /debug trace

... that illustrates your problem, to help us help you.
Justin


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 8:19 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
As DAvid said
Quote:
If the index is not in sequence (or start at 0) then there will be gaps in the data structure hence the nulls


You did something you didn't mean to with index of your list.

Eg:
If I set index = 50 to insert an element in my list, I'll get a 51 sized list having 50 null to the firsts elements

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 8:21 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
As I understand this you are using the primary key column as the index column? This cant work. The index has to start from zero again for each collection. The index column means exactly this - the position of your item in the collection.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2003 6:27 am 
Newbie

Joined: Mon Dec 08, 2003 1:09 am
Posts: 4
Hi,
Thanx for the suggestion. It worked.

Still do that mean I should have a column in the table running from 0 to X to have it represented in List or is there any other feasible solution.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2003 1:00 am 
Newbie

Joined: Mon Dec 08, 2003 1:09 am
Posts: 4
Hi,

I did use the primary key as index in the child table.

Now I have a seperate column in my child table for the index and for each record in my parent ,I have corresponding n records in my child with the index column starting from 1 to n.(one-to-many relationship)

Does this mean that
1 . I can use List only in situation where the child collection index strictly
starts from 0 to n

2 . I should have a seperate column in the child table which holds the
index.

If the answer to the above questions is "YES", then I feel that hibernate forces me to have a index column in all my child tables which should not be the case.

There should be some feasible solution to the problem.

It would be highly appreciated if someone could give an answer to the problem and also explain the different scenarios when it is most effiicient to use List , Set, Map and Bag. I am clear with the basics of List , Set , Bag and Map.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2003 3:15 am 
Regular
Regular

Joined: Tue Sep 02, 2003 5:09 pm
Posts: 81
Location: Whitefish Montana
I am not qualified to give you any hard and fast rules but with or without hibernate, I would tend toward a set when I have unique values (equal/hashcode overidden) or when natural ordering is important. I would tend toward a map when lookups based on a unique key value are important. Another benefit of a map is that it can be naturally ordered. I would tend toward a list when lookups by an index are useful. If a good hashcode can be developed lookups by a key can be pretty fast and unlike a map or set, indexed access is still possible. Ordering for a list will be by sorting. A bag is strictly Hibernate and is similar to a collection. I have not yet used a bag so I can't point to a good use case.

For the most part I would use whatever makes sense in Java to support an application and get my database/hibernate mapping to support that.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2003 11:03 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Mubeena wrote:
Does this mean that
1 . I can use List only in situation where the child collection index strictly
starts from 0 to n

as per the List semantic.

Mubeena wrote:
2 . I should have a seperate column in the child table which holds the index.

yes

Mubeena wrote:
If the answer to the above questions is "YES", then I feel that hibernate forces me to have a index column in all my child tables which should not be the case.

It sounds like you didn't want List semantic.

Mubeena wrote:
It would be highly appreciated if someone could give an answer to the problem and also explain the different scenarios when it is most effiicient to use List , Set, Map and Bag. I am clear with the basics of List , Set , Bag and Map.

The refence doc talks about that in several places (search map, list etc...), have a special look at chapter 13.

_________________
Emmanuel


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.