0

I have an object MainObject which is related to around 20 other objects. These 20 other objects - RelatedObject1, RelatedObject2, etc. The relationship with the MainObject is defined as follows.

1 instance MainObject - > N instances of RelatedObject_1 1 instance MainObject - > N instances of RelatedObject_2 1 instance MainObject - > N instances of RelatedObject_3 . . . 1 instance MainObject -> N instances of RelatedObject_20.

Now all the relationship here is "HAS - A" relationship and not a "IS-A" relationship. It is not a parent-child relationship.

The RelatedObjects have their independent transactional functional flow in the system. Now should I create Bags/Sets/Lists/ for these 20 related Objects in my MainObject.hbm.xml. for accessing their data from the MainObject.

If I create a bag/set/list - I want to ensure that data is not Saved/Persisted in Database when I Save or Update the MainObject. Using which property makes sense in this scenario.

Nisha_Roy
  • 424
  • 2
  • 9
  • 15

1 Answers1

1

use

  • bag when unordered and possibly duplicates
  • set when unordered and no duplicates
  • list when ordered and possibly duplicates

and set cascade="none" to prevent cascading any operation (save, update, ...)

Firo
  • 30,626
  • 4
  • 55
  • 94
  • But since the transactional flow of the related entities are independent, Is it a good practice to include the bags for the same? Will this not increase the Object graph? – Nisha_Roy Mar 27 '12 at 10:49
  • if you often need the mainobject together with related objects it is easier to have the relationship because you can then use fetch paths to eager load the mainobject with related data. Also Cascade="none" makes sure the related objects are not touched when CUD the main object. – Firo Mar 27 '12 at 16:51