Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Hibernate return null or empty collection if table in relation is empty?

Tags:

java

hibernate

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;
  ...
}

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())
{
     ...
}

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

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?

like image 970
Andreas Andreou Avatar asked Oct 13 '15 14:10

Andreas Andreou


People also ask

Is an empty list considered null?

An empty collection isn't the same as null . An empty collection is actually a collection, but there aren't any elements in it yet. null means no collection exists at all.

Is it better to return empty list or null?

Empty collection. Always. It is considered a best practice to NEVER return null when returning a collection or enumerable.

Can Hibernate Criteria List return null?

Fetch a Single Result If you want to obtain a single Object reference instead of a List, the uniqueResult() method on the Criteria object returns an object or null. If there is more than one result, the uniqueResult() method throws a HibernateException .


1 Answers

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.

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:

  • 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)

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.).

like image 154
Thomas Avatar answered Sep 19 '22 11:09

Thomas