Reputation: 264
Notice how in Example 7.2, “Collection mapping using @OneToMany and @JoinColumn” the instance variable parts was initialized with an instance of HashSet. This is the best way to initialize collection valued properties of newly instantiated (non-persistent) instances.
I've tried debugging in several ways, but I didn't get a clear answer. What is the reason?
Upvotes: 0
Views: 186
Reputation: 2558
Imho it has done just to prevent possible NullPointerException
s within industrial code. Such an initialization guaratees collection existence coz you can't know how and by whom your entities will be used in the future.
and as an affirmation is the next to your quotation sentence:
When you make the instance persistent, by calling persist(), Hibernate will actually replace the HashSet with an instance of Hibernate's own implementation of Set
Upvotes: 2