简体   繁体   English

为什么它是初始化新实例化(非持久)实例的集合值属性的最佳方式?

[英]Why is it the best way to initialize the collection value property of a newly instantiated (non-persistent) instance?

See https://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/collections.html#collections-persistent请参阅https://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/collections.html#collections-persistent

Notice how in Example 7.2, “Collection mapping using @OneToMany and @JoinColumn” the instance variable parts was initialized with an instance of HashSet.请注意示例 7.2,“使用 @OneToMany 和 @JoinColumn 的集合映射”中,实例变量部分是如何使用 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?是什么原因?

Imho it has done just to prevent possible NullPointerException s within industrial code.恕我直言,它只是为了防止工业代码中可能出现的NullPointerException 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当你使实例持久化时,通过调用persist(),Hibernate实际上将用Hibernate自己的Set实现的实例替换HashSet

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM