I have inherited some Java code that uses Hibernate. Some people using this code now report that they get NullPointerExceptions everywhere.
I was able to track this and found that when executing a query that pulls a list of objects from a database that has a list of objects (which are retrieved from another table), it seems that hibernation leaves holes in the list (NULL values). So the list might look something like this:
Object Object NULL Object
The code we use to retrieve information from the database:
List<PrinterGroup> groups = this.getSession().createQuery( "from PrinterGroup" ).list();
And then inside each PrinterGroup there is a list of filters that have NULL values ββin them.
While I could get around and find each instance, we are looping over this list and adding a NULL check, I think this is a fix in the band, and there should be a way to tell Hibernate not to pull out the null values.
EDIT:
EDIT2:
So the database seemed confusing. A PrinterGroup β Filter relationship is a one-to-many relationship. So PrinterGroups has a list of filters. The problem is that the filter list has zero values ββin it when it exits the database (by the way, the database does not have zero values), and the list looks like the one shown above.
EDIT3:
Here is a link to relavant picese in 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 simple POJO mapping.
java hibernate
Patrick mcdaniel
source share