java Hibernate 中的空集合与空集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1454846/
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-29 16:35:36  来源:igfitidea点击:

Null vs empty collections in Hibernate

javahibernate

提问by skaffman

Say I have the following Hibernate-mapped class:

假设我有以下 Hibernate 映射类:

public class ClassA {       
   @OneToMany(fetch=EAGER)
   private List<ClassB> bList;
}

When I read an object of ClassAfrom a Hibernate session, the bListfield is initialized with a PersistentListobject, as expected.

当我ClassA从 Hibernate 会话中读取对象时,该bList字段PersistentList按预期使用对象初始化。

I find myself with a requirement where in situations where the list is empty, I need Hibernate to initialize the bListfield to null, rather than with an empty PersistentList. In theory, Hibernate has the information it needs to do this, since the fetch on the list is eager. The problem is that according to section 6.1 of the Hibernate docs:

我发现自己有一个要求,在列表为空的情况下,我需要 Hibernate 将bList字段初始化为null,而不是使用空的PersistentList. 理论上,Hibernate 拥有执行此操作所需的信息,因为列表上的获取是急切的。问题是,根据Hibernate 文档的第 6.1 节

Due to the underlying relational model, collection-valued properties do not support null value semantics. Hibernate does not distinguish between a null collection reference and an empty collection.

由于底层关系模型,集合值属性不支持空值语义。Hibernate 不区分空集合引用和空集合。

This makes perfect sense, but I'm hoping someone can come up with a cunning ruse to overcome this limitation. I'm thinking perhaps some listener/callback mechanism might allow me to replace empty lists with null references.

这是完全有道理的,但我希望有人能想出一个狡猾的诡计来克服这个限制。我在想也许某些侦听器/回调机制可能允许我用空引用替换空列表。

采纳答案by amischiefr

Have you tried to check in the getbList() method? You could do:

您是否尝试过检查 getbList() 方法?你可以这样做:

if(bList.isEmpty()) 
    return null;
return bList;

Hibernate will always create an object for your references, but you are allowed to control the data inside of the getter and setters. If the list has 0 elements you can always return null.

Hibernate 将始终为您的引用创建一个对象,但您可以控制 getter 和 setter 中的数据。如果列表有 0 个元素,您总是可以返回 null。

回答by matt b

I'm curious why you consider this a "limitation' - does a null bListactually have a different meaningto your application than an empty bList?

我很好奇你为什么认为这是一个“限制” - null对你的应用程序bList实际上有不同的含义bList吗?

I think that in most areas, a null collection and an empty collection have the same semantic meaning, which I would guess is why the Hibernate developers sought to limit Hibernate to only using one. Doesn't make much sense to always check if (bList == null || bList.isEmpty)if the two always end up meaning the same thing.

我认为在大多数领域,空集合和空集合具有相同的语义,我猜这就是 Hibernate 开发人员试图将 Hibernate 限制为仅使用一个的原因。总是检查if (bList == null || bList.isEmpty)两者是否总是意味着相同的事情并没有多大意义。

回答by Damo

For handling in your code the obvious way is in the getter, however that doesn't help you if you want to evaluate it in HQL.

对于在您的代码中处理,显而易见的方法是在 getter 中,但是如果您想在 HQL 中对其进行评估,这对您没有帮助。

Two ideas:

两个想法:

  • A constructor that sets it to NULL if empty.
  • A @PostLoad / @PostConstructmethod that does the same.
  • 如果为空,则将其设置为 NULL 的构造函数。
  • @PostLoad / @PostConstruct做同样事情的方法。