title: JPA - query multiple oneToMany
body: <p>I want to select an entity <code>Person</code> from the database (JPA with Hibernate). <code>Person</code> has has two (or more) oneToMany-Relationships, e.g. <code>Address</code> and <code>Phone</code>.</p>
  
  <p>How would I <em>fully</em> restore the instance of <code>Person</code> and all related entities of <code>Address</code> and <code>Phone</code>. E.g. the focal Person has two Addresses and three Phones?</p>
  
  <p>(I can <code>persist</code> the instance of <code>Person</code> with one command, but how can I load it?)</p>
  
  <pre><code>@Entity
  public class Person implements Serializable { 
     @Id @GeneratedValue
     private int id;
     private String name;
  
     @OneToMany(mappedBy="person")
     private List&lt;Address&gt; addresses = new ArrayList&lt;Address&gt;();
  
     @OneToMany(mappedBy="person")
     private List&lt;Phone&gt; phones = new ArrayList&lt;Phone&gt;();        // plus getter and setter
  }
  
  @Entity
  public class Address implements Serializable { 
      @Id @GeneratedValue
      private int id;
  
      @ManyToOne
      private Person person;
      private String onestring;         // plus getter and setter
  }
  
  @Entity
  public class Phone implements Serializable { 
      @Id @GeneratedValue
      private int id;
  
      @ManyToOne
      private Person person;
      private String anotherstring;         // plus getter and setter
  }
  </code></pre>
  
  <p><strong>EDIT</strong>: I tried two ways suggested below. First try with calling getter:</p>
  
  <pre><code>@Stateless
  public class PersonManager {
  
  @PersistenceContext
  private EntityManager em;
  
  public Person getPerson(int id) {
     Person person = em.find(Person .class, id);
     person.getAddresses ();
     person.getPhones();
     return person;
  }
  </code></pre>
  
  <p>Result: both lists are not initialized.</p>
  
  <p>Second way:</p>
  
  <pre><code>@NamedQuery(name="PersonAddressesPhones", 
     query = "SELECT p FROM Person p " +
             "left join fetch p.addresses " +
             "left join fetch p.phones " +
             "WHERE p.id = :id")
  </code></pre>
  
  <p>Result: This causes the exception: 
  <code>org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags</code></p>