My entity class looks like this:
Code:
public class Customer {
private Set<Image> _images;
@OneToMany(...)
public Set<Image> getImages() {
return _images;
}
public void setImages(Set<Image> images) {
_images = images;
}
public void add(Image image) {
if (_images == null) {
_images = new HashSet<Image>();
}
_images.add(image);
}
}
But documentation recommends association field initialization:
Code:
public class Customer {
private Set<Image> _images = new HashSet<Image>();
...
}
Why do I need association field initialization?
For example, I can use the customer.getImages().add(image) instead customer.add(image) for a new entity. If an entity is loaded from the database then Hibernate initializes the field.
Is this related to restriction "return same collection instance from getter if association accessed through getter / setter"?