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?
nullinstead of an empty list is a bad practice.