Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

One to many by Set


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.

  1. package com.Kreationnext;
  2. import java.util.List;
  3. public class Question {
  4. private int id;
  5. private String qname;
  6. private Set<Answer> answers;
  7. //getters and setters
  8. }

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

  1. package com.Kreationnext;
  2. public class Answer {
  3. private int id;
  4. private String answername;
  5. private String postedBy;
  6. //getters and setters
  7. }
  8. }

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.

  1. <set name=“answers” cascade=“all”>
  2.           <key column=“qid”></key>
  3.           <one-to-many class=“com.Kreationnext.Answer”/>
  4. </set>

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.

Comments

comments