Hibernate glues NULL values ​​to a list

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:

  • We use Hibernate 3.2.2

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.

+6
java hibernate
source share
5 answers

Is this collection mapped to <list> (or another indexed collection) and <list-index> ?

All collection mappings, except collection and package semantics, require an index column in the collection table. An index column is a column that maps to an array index, list index, or key .... The index of an array or list is always of type integer and displayed using the element. The displayed column contains consecutive integers that are, by default, numbered from zero.

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

+12
source share

So, PrinterGroup objects themselves are not zeros, but have null references to objects of type Filter? Or non-zero references to Filter Lists containing null filters?

The obvious answer is that the database contains a many-to-many relationship between PrinterGroup and the filter and is null for filters, for example:

  select * from printer_group_filter; id printergroup_id filter_id 1 1 1 2 1 null 3 1 2 4 2 1 5 2 null 

If it is possible that the filter will be zero, then this is your requirement. Or you can use the NullObject idiom to make the "null filter" a real object, although this will not be easy with Hibernate.

If the filter cannot be null, correct your data, add a null-null constraint on the many-to-many number, and correct the input check to prevent more zeros from being added.

+1
source share
 List<PrinterGroup> groups = this.getSession().createCriteria( PrinterGroup.class ).add(Restrictions.isNotNull("filters")).list(); 
+1
source share

You must either check for null or reorder the list to get rid of the null value. His pain.

0
source share

How to easily filter null filters using HQL

 List<PrinterGroup> groups = this.getSession().createQuery( "Select pg from PrinterGroup pg where pg.filter is not null" ).list(); 

I assume your property is called 'filter'

0
source share

All Articles