如果关联表为空,Hibernate是否会返回null或空集合? [英] Does Hibernate return null or empty collection if table in relation is empty?

查看:180
本文介绍了如果关联表为空,Hibernate是否会返回null或空集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对某事感到好奇.假设我们在 Employee Phone 之间有一个简单的关系:

I was curious about something. Let's say we have a simple relation between Employee and Phone:

@Entity
public class Employee {
  @Id
  @Column(name="EMP_ID")
  private long id;
  ...
  @OneToMany(mappedBy="owner")
  private List<Phone> phones;
  ...
}
@Entity
public class Phone {
  @Id
  private long id;
  ...
  @ManyToOne(fetch=FetchType.LAZY)
  @JoinColumn(name="OWNER_ID")
  private Employee owner;
  ...
}

让我们假设 Employee 没有电话, Phone 表中没有条目. 如果我要编写一段代码来获取 Employee 的电话并出于任何原因对其进行迭代

Let's assume that an Employee has no phones, no entries in the Phone table. If I were to have a piece of code that gets the phones of an Employee and iterates over them for whatever reason

for (Phone phone : employee.getPhones())
{
     ...
}

吸气剂会重新调零NULL还是一个​​空Collection,这种吸气策略会发挥作用.

Would the getter retun NULL or an empty Collection and would the getching strategy play a part.

如果我没记错的话,冬眠有自己使用代理进行收集的实现,并且对于 LAZY 提取,它会使用其中之一进行实例化,并在需要时从表中检索数据(正确,如果我错了).在调用getter时也会尝试从表中检索数据,结果是得到一个空集并返回一个空集合. (这就是我的想法).还是应该始终检查getter的结果是否为 NULL ?

If I remember correctly, hibernate has its own implementation of collection using proxies and for LAZY fetch, it instantiates with one of those and when needed retrieves the data from the table (correct If I am wrong). So would at the time the getter is called try to retrieve the data from the table, get an empty set as a result and return an empty collection. (This is what I think). Or should I always check if the result of the getter is NULL or not?

推荐答案

由于默认情况下这些集合是惰性的,因此employee.getPhones()应该返回该集合的代理(例如PersistentList或类似名称),该代理将在您访问时加载列表元素列表.

Since those collections are lazy by default employee.getPhones() should return a proxy for that collection (e.g. PersistentList or similar) which loads the list elements when you access the list.

此外,由于Phone是该关系的所有者,因此Hibernate将不知道是否有任何员工用的电话,因此必须假定该列表存在-尽管可能空的.也就是说,由于以下原因,Hibernate返回null并没有多大意义:

Additionally, because Phone is the owner of the relation Hibernate won't know whether there are any phones for an employee or not so it has to assume the list exists - although it might be empty. That said it wouldn't make much sense for Hibernate to return null since:

  • 休眠模式需要先尝试加载手机,然后再查看
  • 要实现对集合getPhones()的延迟加载,不得返回null,而是返回一个代理
  • 无论如何,返回null都是不好的做法(列表仍然存在,只是空的)
  • 如果列表为空,则无法添加电话,让Hibernate使用级联等方式自动保留该更改(感谢 Gimby 指出这一点)
  • Hibernate would need to try and load the phones first to see that there are none
  • to implement lazy loading of the collection getPhones() must not return null but a proxy
  • returning null would be bad practice anyways (the list would still exist, it's just empty)
  • if the list was null you could not add a phone and let Hibernate use cascading etc. to automatically persist that change (thanks to Gimby for pointing that out)

使用紧急加载不应对此进行更改. Hibernate会知道该员工没有电话,但返回null而不是一个空列表,这也表示没有电话,这仍然毫无意义(请考虑允许为已加载的员工添加电话,代码有所不同如果将null用作急切的获取操作,则不需要它们.)

Using eager loading shouldn't change that. Hibernate would know that there is no phone for the employee but returning null instead of an empty list, which also would express that there are no phones, would still make little sense (think of allowing to add phones for a loaded employee, differences in code where you'd not need them if null was used for eager fetching, etc.).

这篇关于如果关联表为空,Hibernate是否会返回null或空集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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