Stefan Zauner

Greenhorn
+ Follow
since Feb 23, 2010
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Stefan Zauner

Hi all!

Since I also ran into this problem and since it does not seem to be solved yet, I registered with the forum to share my experiences.

At first it is to say that Sandeep Vaid is right with his explanation, but some of you misinterpreted his answer. In fact the number of elements in the resulting java list does not directly depend on the number of records in the database. By using the <list>-element in a mapping you tell hibernate that you want to get a list with indexed containers. You know, Java lists can be accessed by using the get()-method and an index. The sub-element <list-index> tells Hibernate which column stores the information where to put the retrieved records in the resulting java list. Let's assume you have the following table:

Children

child_id name parent_id birthOrder age
1 ____ Sepp ___ 2 _____ 0 _____ 5
2 ____ Maria ___ 2 _____ 1 _____ 9
3 ____ Hans ___ 2 _____ 2 _____ 11

We can reuse the mapping of Giuseppe for the my example:



Since Hibernate assumes by default that you start counting at zero this mapping gives us exactly what we expect:
Java-List: [Sepp, Maria, Hans]

If you start the birthOrder with 1 you run into our problem. But for that case Giuseppe's solution is absolutely suitable (as long as the numbering has no "holes").
So what if you do not have the column birthOrder but only the column age? Of course you can also use this one for your <list-index>-element. Hibernate will then assume that you want a list that has Sepp on position 5, Maria on 9 and Hans on 11. The resulting gaps in the list will be filled with null values:
[null, null, null, null, null, Sepp, null, null, null, Maria, null, Hans]

You may use the base-attribute to eliminate the leading null-values, but the most you can get is:
[Sepp, null, null, null, Maria, null, Hans]

The proper solution is simple. Do not use the <list>-element, but use a <bag>. Bags are not ordered and may contain duplicates, but they also map to the java interface List and you are able to let Hibernate sort them by using the order-by-attribute.
At last the example to my solution:



Kind regards