java Hibernate 将 NULL 值粘贴到列表中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3268241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:04:22  来源:igfitidea点击:

Hibernate sticking NULL values into list

javahibernate

提问by Patrick McDaniel

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.

我继承了一些使用 Hibernate 的 Java 代码。一些使用此代码的人现在报告说他们到处都是 NullPointerExceptions。

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:

我已经能够追踪到这一点,并发现当我们执行从数据库中提取对象列表的查询时,该查询具有一个对象列表(从不同的表中提取)Hibernate 似乎在列表(空值)。因此该列表可能类似于:

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.

然后在每个 PrinterGroup 内部是一个过滤器列表,其中包含 NULL 值。

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.

虽然我可以四处寻找每个实例,如果我们遍历这个列表并添加一个 NULL 检查,我觉得这是一个创可贴的修复,并且必须有一种方法告诉 Hibernate 不要引入空值。

EDIT:

编辑:

  • We are using Hibernate 3.2.2
  • 我们正在使用 Hibernate 3.2.2

EDIT2:

编辑2:

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.

所以数据库似乎很混乱。PrinterGroup -> Filter 关系是一对多关系。所以 PrinterGroups 有一个过滤器列表。问题是过滤器列表在从数据库中出来时有空值(顺便说一下,数据库中没有空值)并且列表看起来像上面那样。

EDIT3:

编辑3:

Here is the mapping relavant picese in the PrinterGroup HBM

这是 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.

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

回答by matt b

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

此集合是否与<list>(或其他索引集合)和<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.

所有集合映射,除了那些具有 set 和 bag 语义的映射,在集合表中都需要一个索引列。索引列是映射到数组索引、列表索引或映射键的列。...数组或列表的索引始终是整数类型,并使用元素进行映射。映射列包含默认从零开始编号的连续整数。

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 Listwith empty elements at the expected places.

我会想象,当使用索引集合时,如果您的索引列中有间隙(即它有像0, 1, 3, 之类的值7),Hibernate 会List在预期的位置用空元素填充结果。

回答by Daniel Moura

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

回答by tpdi

Ok, so the PrinterGroup objects are not themselves null, but they have null references to objects of type Filter? Or non-null references to Lists of Filters, which contain null Filters?

好的,那么 PrinterGroup 对象本身不是空的,但它们对过滤器类型的对象有空引用?或者对包含空过滤器的过滤器列表的非空引用?

The obvious answer is that the database contains a many-to-many relation between PrinterGroup and Filter, and has database null values for filters, e.g.:

显而易见的答案是数据库包含 PrinterGroup 和 Filter 之间的多对多关系,并且过滤器具有数据库空值,例如:

 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's possible for a filter to be null, well, that's your requirement. Or you can use the NullObject idiom to make "the null filter" an actual object, though this isn't going to be straightforward with Hibernate.

如果过滤器可能为空,那么这就是您的要求。或者,您可以使用 NullObject 习语使“空过滤器”成为实际对象,尽管这对于 Hibernate 来说并不简单。

If it's not possible for a filter to be null, fix your data, add a not-null constraint to the many-to-many, and fix your input validation to prevent the addition of more nulls.

如果过滤器不可能为空,请修复您的数据,向多对多添加非空约束,并修复您的输入验证以防止添加更多空值。

回答by Greg

How about just filtering out null filters using HQL

如何使用 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'

我假设你的财产被称为“过滤器”

回答by hvgotcodes

You should either null check, or you have to reorder the list to get rid of the null. Its a pain.

您应该检查空值,或者必须重新排序列表以消除空值。它的痛苦。