• 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

Hibernate returning List with NULL values

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Any idea?

Thanks in advance!
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Mark
 
Jazzy Sanchez
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does your database contain null values? There is no reason for hibernate to do this unless its reflective of what is in your database.

Have you checked for thrown exceptions? And looked in the log files?
 
Jazzy Sanchez
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Database contains null fields(properties) but Hibernate is returning null objects which should not be the case.

List contains like:

Null
Object
Null
Null
Null
Object
Null

So when i iterate it, iterator will return true for hasNext() method.

There's no error on logs.

Thanks!
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So lazy loading is false, what is the fetching strategy? What is that set to?

Mark
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there is no reason for hibernate to do this, are you add some null object in the collection when you save the object?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am facing the same problem. Have anybody find the solucion to this?

My database table doesnt have null values.

Thanks
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.



 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I solved the problem by adding base="1" attribute into <list> tag:


Regards,

Giuseppe.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stefan -- Thanks for the very clear explanation. Just what I needed to know!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic