title: How to have Zero To Many relationship in Hibernate
body: <p>I have a class called <code>Member</code> that has a few <code>@OneToMany</code> relationships with other classes. I can insert only one <code>Member</code> with empty <code>OneToMany</code> relationships. Upon inserting the second <code>Member</code> I receive following exception. </p>
  
  <pre><code>ERROR: Duplicate entry '' for key 'UK_7pn834d04yft1rkvpqf0viuyc'
  </code></pre>
  
  <p><strong>Code</strong></p>
  
  <pre><code>@Entity
  public class Member {
      @Id
      @Column(name = "username", nullable = false, unique = true)
      private String email;
      @OneToMany(mappedBy = "friend")
      private Set&lt;Friendship&gt; friends;
      ....
  </code></pre>
  
  <p>To insert <code>Member</code>, I am adding <strong>empty set of friends</strong> to it. I suspect this and suppose need to have <code>ZeroToMany</code> relationship but could not find its annotation in <a href="https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/collections.html" rel="nofollow">Hibernate documentation</a>.</p>
  
  <p><strong>Update</strong></p>
  
  <pre><code>Member member = new Member(email, new HashSet&lt;Friendship&gt;());
  </code></pre>
  
  <p>Constructor</p>
  
  <pre><code>public Member(String email, Set&lt;Friendship&gt; friends) {
          super();
          this.email = email;
          this.friends = friends;
      }
  </code></pre>
  
  <p>toString</p>
  
  <pre><code> member:Member [email=jack.moore1@outlook.com , friends=[]]
  </code></pre>
  
  <p>console</p>
  
  <pre><code>insert 
          into
              Member
              (username) 
          values
              (?)
  </code></pre>