Javatpoint Logo

91-9990449935

 0120-4256464

Set One to many

If the persistent class has set object that contains the entity reference, we need to use one-to-many association to map the set element. We can map this list object by either set.

It is non-index based and will not allow duplicate elements.

Let's see the persistent class that has set objects. In this case, there can be many answers for a question and each answer may have its own informations that is why we have used set element to represent a collection of answers.

The Answer class has its own informations such as id, answername, postedBy etc.

The Question class has set object that have entity reference (i.e. Answer class object). In such case, we need to use one-to-many of set to map this object. Let's see how we can map it.


Example of mapping set in collection mapping by one to many association

To understand this example, you may see the bag one-to-many relation example. We have changed only bag to set in the hbm file and ArrayList to HashSet in the Store class.