title: Java Hibernate - OneToMany relationship returning a null object (annotations)
body: <p>I'm currently struggling with a mapping of one to many using annotations in hibernate, whenever I get an object, the set of items returned from the related table is always null (even when I can see in the database there are corresponding relationships with data). I have some many to one relationships in the same class which are working fine.</p>
  
  <p>Other unrelated code omitted for readability</p>
  
  <p>I have two tables, where a single member, can have 0 or more member membership periods:</p>
  
  <pre><code>CREATE TABLE member (
      member_id INT NOT NULL AUTO_INCREMENT
      PRIMARY KEY (member_id)
  )
  
  CREATE TABLE member_membership_period (
      member_membership_period_id INT NOT NULL AUTO_INCREMENT ,
      member_id INT NOT NULL ,
      test_column VARCHAR(45) NOT NULL ,
      PRIMARY KEY (member_membership_period_id) ,
      INDEX member_membership_period_member_idx (member_id ASC) ,
      CONSTRAINT member_membership_period_member
          FOREIGN KEY (member_id)
          REFERENCES member (member_id)
          ON DELETE NO ACTION
          ON UPDATE NO ACTION)
  </code></pre>
  
  <p>The Member class maps to the member table:</p>
  
  <pre><code>@Entity
  @Table(name="member")
  public class Member implements Serializable  {
  
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      @Column(name = "member_id")
      private int id;
  
      @OneToMany
      @JoinColumn(name = "member_id")
      @ForeignKey(name = "member_membership_period_member")
      private Set&lt;MemberMembershipPeriod&gt; memberMembershipPeriods = new HashSet&lt;MemberMembershipPeriod&gt;();
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public Set&lt;MemberMembershipPeriod&gt; getMemberMembershipPeriods() {
          return memberMembershipPeriods;
      }
  
      public void setMemberMembershipPeriods(Set&lt;MemberMembershipPeriod&gt; memberMembershipPeriods) {
          this.memberMembershipPeriods = memberMembershipPeriods;
      }
  }
  </code></pre>
  
  <p>And the MemberMembershipPeriod class maps to the member_membership_period table</p>
  
  <pre><code>@Entity
  @Table(name="member_membership_period")
  public class MemberMembershipPeriod implements Serializable {
  
      @Id
      @GeneratedValue(strategy= GenerationType.IDENTITY)
      @Column(name = "member_membership_period_id")
      private int id;
  
      @Column(name = "test_column")
      String testColumn;
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getTestColumn() {
          return testColumn;
      }
  
      public void setTestColumn(String testColumn) {
          this.testColumn = testColumn;
      }
  }
  </code></pre>
  
  <p>My DAO</p>
  
  <pre><code>public class MemberDaoImpl extends AbstractDAO&lt;Member&gt; implements MemberDao {
  
      public MemberDaoImpl(SessionFactory factory) {
         super(factory);
      }
  
      @Override
      public List&lt;Member&gt; getAllMembers() {
          Query query = currentSession().createQuery("from Member");
          return list(query);
      }
  
      @Override
      public Member getMemberById(int id) {
          return get(id);
      }
  }
  </code></pre>
  
  <p>Implementation of get(id) (Part of drop wizards hibernate package)</p>
  
  <pre><code>protected E get(Serializable id) {
      return (E) currentSession().get(entityClass, checkNotNull(id));
  }
  </code></pre>
  
  <p>Any help provided would be greatly appreciated, I'm starting to lose the will to live over this! </p>
  
  <p>Other tech being used is DropWizard (which does the hibernate configuration) and MySQL</p>