Quote:
My question is when we declare parts attribute as a Set, do we have to initialize it in the declaration (as in line 3)?
No you don't have to. But you have to watch out for 'null' in code like:
Code:
product.getParts().add(part);
Personally, I prefer this:
Code:
public Set getParts() {
if (parts == null) parts = new HashSet();
return parts;
}
It avoids the creation of a new HashSet that is just replaced by Hibernate in a call to setParts() every time an existing object is loaded from the database. In practice I don't think it matters which method you use, but I would get crazy if I had to check for 'null' every time I wanted to use a collection.