We have seen that Hibernate supports both collection of Entities and collection of values / components. Here is a short summary of the points of note when working with the two.
Having gone through a lot of documentation, blogs and codes involving complex life cycle of the Collections of entities, I would like to repeat the advice of Gavin King
"Collection of Entity References is a feature and not a requirement. if it gets too difficult don't do it."
Criteria | Value Types | Entities |
---|---|---|
Representation | These are easily represented using <element> in case of simple types or <composite-element> if the collection is composed of components. The collection table must be specified here | These are represented using <one-to-many> elements. The table name can be skipped here as hibernate can find the table for the mapped entity easily. |
Type of Data | The Collection is made of Value types while the Owner is a mapped entity. | Both the Collection owner and the Collection elements are Entities, |
Relation ships And Life cycles | The value types exist for the Owner. They have no independent life-cycle. All changes for the Owner directly impact on the Collection i.e. there is no need for any cascade settings. They apply by default. Life-cycle management for Hibernate is as easy as it ever gets. | Both being entities, the Collection owner and the Collection members have their own life cycle. Either the developer needs to manage the updates or saves or he can guide Hibernate with appropriate cascade settings. Inverse settings may be needed to streamline db hits in case of bidirectional relations. |
Shared References | No possibility of shared references here. | There is always the facility to expand the entity model and allow shared references from other entities to the Collection members. Probably the one huge requirement that justifies all the above headache |
"Collection of Entity References is a feature and not a requirement. if it gets too difficult don't do it."
No comments:
Post a Comment