Hibernate将NULL值粘贴到列表中 [英] Hibernate sticking NULL values into list

查看:128
本文介绍了Hibernate将NULL值粘贴到列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了一些使用Hibernate的Java代码。一些使用此代码的人现在报告说他们正在全方位获取NullPointerException。

我已经能够跟踪它并发现当我们执行一个从数据库中提取对象列表的查询时,它有一个对象列表从不同的表中拉出来)Hibernate似乎在列表中留下空洞(NULL值)。因此,列表可能如下所示:

  Object 
Object
NULL
Object

我们用来从数据库中提取信息的代码是:

 列表< PrinterGroup> groups = 
this.getSession()。createQuery(from PrinterGroup).list();

然后在每个PrinterGroup里面都有一个过滤器列表,其中有NULL值。



虽然我可以绕过去查找每个实例,但我们在这个列表中循环并添加一个NULL检查,我觉得它是一个bandaid修复程序,并且必须有一种方法来告诉Hibernate不要拉空值。



编辑:




  • 我们使用Hibernate 3.2.2


EDIT2:因此数据库似乎令人困惑。 PrinterGroup - > Filter关系是一对多的关系。所以PrinterGroups有一个过滤器列表。问题是,过滤器列表中有空值,当它出现在数据库中时(数据库中没有空值),列表出现如上所示。



EDIT3:

以下是PrinterGroup HBM中映射相关的picese

 < subclass name =PrinterGroupdiscriminator-value =PG> 
< list name =filters
lazy =true
table =PG_FILTER
inverse =false
cascade =all-delete-孤儿>

< key>
< column name =PG_IDnot-null =false/>
< / key>
< index column =LISTPOSITION/>
< / list>

Filter是一个非常基本的POJO映射。

解决方案

这个集合是用 < list> (或其他索引集合)和< list-index>
lockquote>

所有集合映射(除set和bag语义之外)都需要集合中的索引列表。索引列是映射到数组索引或列表索引或Map键的列。 ...数组或列表的索引始终为整型,并使用该元素进行映射。


我会想象当使用索引集合时,如果您的索引列中存在空格(即它具有像 0 1 3 7 ),Hibernate将在预期的地方填充结果为 List 的空元素。 / p>

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.

解决方案

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.

这篇关于Hibernate将NULL值粘贴到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆