File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Hibernate returning List with NULL values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate returning List with NULL values" Watch "Hibernate returning List with NULL values" New topic
Author

Hibernate returning List with NULL values

Jazzy Sanchez
Ranch Hand

Joined: Apr 02, 2006
Posts: 35
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!
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Jazzy Sanchez
Ranch Hand

Joined: Apr 02, 2006
Posts: 35
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
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 ]
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

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

Joined: Apr 02, 2006
Posts: 35
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
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

So lazy loading is false, what is the fetching strategy? What is that set to?

Mark
yo loo
Greenhorn

Joined: Jul 16, 2007
Posts: 3
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?
Darwin Ramos Cuervo
Greenhorn

Joined: Jan 30, 2008
Posts: 1
I am facing the same problem. Have anybody find the solucion to this?

My database table doesnt have null values.

Thanks
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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
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
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
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
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
I solved the problem by adding base="1" attribute into <list> tag:


Regards,

Giuseppe.
Stefan Zauner
Greenhorn

Joined: Feb 23, 2010
Posts: 1
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
Steve Hiller
Greenhorn

Joined: Jul 25, 2008
Posts: 7
Stefan -- Thanks for the very clear explanation. Just what I needed to know!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate returning List with NULL values
 
Similar Threads
mixed null properties in Restrictions?
Update Single value in a row
Hibernate returns List with NULL
List that contains null values
Improving An Algorithm