I have a one-to-many relationship. As i retrieve the Parent object, hibernate returns the Parent object and List of Child objects. The problem is, the List contains some null values so there's some error when iterating through it.
When are you iterating through them. Is it while the Session is still open. Because Hibernate will sometimes fill in a Collection with proxy objects, that if you try to access while not having a Session open will give you no data, just errors.
The session is close and lazy loading is set to false. I am now iterating through a list, getting all objects and store it to a new list. To get rid of null values.
I just wonder why hibernate is returning a List with size of 10 where the objects are just actually 3.
Thanks again!
Jazzy Sanchez
Ranch Hand
Joined: Apr 02, 2006
Posts: 35
posted
0
Any idea guys? Hibernate is returning a list that contains NULL values. Should I use utility class to manually clear the values so i can easily iterate through the values?
Thanks!!
[ July 09, 2007: Message edited by: Jazzy Sanchez ] [ July 09, 2007: Message edited by: Jazzy Sanchez ]
Originally posted by Darwin Ramos Cuervo: I am facing the same problem. Have anybody find the solucion to this?
My database table doesnt have null values.
Thanks
I think you should post a new thread for your issue. And please post your mappings and config file, using the CODE tags.
Mark
Miguel Kojiio Nobre
Greenhorn
Joined: May 01, 2008
Posts: 3
posted
0
Hi,
Just to register a solution that works fine for me! I have changed the mapping element <list> to <bag> in hbm file, and I keep using List as my Collection. No more null values after that.
Curso <a href="http://www.t2ti.com/java_starter.php" target="_blank" rel="nofollow"> java iniciante</a> com pdf e v�deo aulas gratuitas!
Sandeep Vaid
Ranch Hand
Joined: Feb 27, 2006
Posts: 390
posted
0
I know the cause of null values but i don;t know the solution yet..
As you are using a java.util.List, the ordering has to be maintained.
say your list element is of size 5 i,e from 0th index to 4th index... Now when you retrieve this list based on some conditions, say you get 1st, 2nd and 4th element.
As it has to maintain the ordering, the return list will be
0 NULL,
1 ELEMENT,
2 ELEMENT,
3 NULL,
4 ELEMENT.
Rahul Babbar
Ranch Hand
Joined: Jun 28, 2008
Posts: 210
posted
0
Sandeep Vaid wrote:I know the cause of null values but i don;t know the solution yet..
As you are using a java.util.List, the ordering has to be maintained.
say your list element is of size 5 i,e from 0th index to 4th index... Now when you retrieve this list based on some conditions, say you get 1st, 2nd and 4th element.
As it has to maintain the ordering, the return list will be
0 NULL,
1 ELEMENT,
2 ELEMENT,
3 NULL,
4 ELEMENT.
Hi
I am just a little confused over your explanation.
Do you mean that if i do an HQL query over a table that has 100 records and it gives back 10 records, Hibernate will return me a list which will have 90 null objects and 10 'real' objects..
Rahul Babbar
Nicholas White
Greenhorn
Joined: Oct 09, 2009
Posts: 1
posted
0
I know this thread is old, but it's high on google...
I've had this problem, and it seems Hibernate is failing to deserialise an object, and so just returns a null. Setting a breakpoint on subclasses of HibernateException doesn't halt, so it's part of the internal logic somewhere.
The problem (in my case) is because the primary key mapping is invalid. I had a composite-key which had nulls for some of the columns (the database itself didn't have a primary key). Try changing(by removing columns) in the hbm.xml file until all the objects appear in the list of returned results.
In the example in this thread, some Objects are returned where Hibernate can create them (i.e. the key mapping for that Object is valid) whereas the nulls are inserted whenever the row can't be deserialised. To debug - add an order-by statement to the Hibernate query so the results are returned in a guarenteed order. Then match the rows in the database (by running the HQL query directly on the database) with the nulls to get an idea of why your Hibernate key is invalid.
Giuseppe Alemanno
Greenhorn
Joined: Feb 06, 2010
Posts: 1
posted
0
I solved the problem by adding base="1" attribute into <list> tag:
Regards,
Giuseppe.
Stefan Zauner
Greenhorn
Joined: Feb 23, 2010
Posts: 1
posted
0
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:
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
Steve Hiller
Greenhorn
Joined: Jul 25, 2008
Posts: 7
posted
0
Stefan -- Thanks for the very clear explanation. Just what I needed to know!