Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate sticking NULL values into list

Tags:

java

hibernate

I have inherited a bit of Java code that uses Hibernate. Some of the people using this code are now reporting that they are getting NullPointerExceptions all over the place.

I've been able to track this down and found that when we execute a query that pulls a list of objects from the database, that has a list of objects (that get pulled from a different table) Hibernate seems to be leaving holes in the list (NULL values). So the list may look something like:

Object
Object
NULL
Object

The code we are using to pull the information out of the database is:

List<PrinterGroup> groups = 
    this.getSession().createQuery( "from PrinterGroup" ).list();

And then inside each PrinterGroup is a list of Filters that have the NULL values in them.

While I could go around and find every instance were we loop over this list and add a NULL check I feel it is a bandaid fix, and there has to be a way to tell Hibernate to not pull null values in.

EDIT:

  • We are using Hibernate 3.2.2

EDIT2:

So the database seemed to be confusing. The PrinterGroup -> Filter relationship is a one to many relationship. So PrinterGroups have a list of filters. The problem is that list of filters has null values in it when it comes out of the database (There are no null values in the database by the way) and the list comes out looking like above.

EDIT3:

Here is the mapping relavant picese in the PrinterGroup HBM

<subclass name="PrinterGroup" discriminator-value="PG">
   <list name="filters"
              lazy="true"
              table="PG_FILTER"
               inverse="false"
                cascade="all-delete-orphan">

        <key>
           <column name="PG_ID" not-null="false"/>
        </key>
        <index column="LISTPOSITION"/>
        <one-to-many class="Filter"/>
     </list>

And the Filter is a pretty basic POJO mapping.

like image 871
Patrick McDaniel Avatar asked Jul 16 '10 19:07

Patrick McDaniel


2 Answers

Is this collection mapped with a <list> (or other indexed collection) and <list-index>?

All collection mappings, except those with set and bag semantics, need an index column in the collection table. An index column is a column that maps to an array index, or List index, or Map key. ... The index of an array or list is always of type integer and is mapped using the element. The mapped column contains sequential integers that are numbered from zero by default.

I would imagine that when using an indexed collection, if your index column has gaps in it (i.e. it has values like 0, 1, 3, 7) that Hibernate would populate the resulting List with empty elements at the expected places.

like image 119
matt b Avatar answered Oct 18 '22 23:10

matt b


List<PrinterGroup> groups = this.getSession().createCriteria( PrinterGroup.class ).add(Restrictions.isNotNull("filters")).list();
like image 26
Daniel Moura Avatar answered Oct 18 '22 23:10

Daniel Moura