title: How to query join one to many in hibernate HQL
body: <p>I try query join tow table customer and mainorder table . when i query always error</p>
  
  <p>-Customer.java</p>
  
  <pre><code>package com.mdr.model;
  
  import java.util.List;
  import java.util.Set;
  
  import javax.persistence.CascadeType;
  //import java.io.Serializable;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.OneToMany;
  import javax.persistence.Table;
  /**
   * 
   * @author Sothon
   *
   */
  @Entity
  @Table(name = "customer")
  public class Customer{
  
  
      @Id
      @GeneratedValue
      @Column(name="id")
      private long id;
      @Column(name="name")
      private String name;
      @Column(name="person")
      private String person;
      @Column(name="mail")
      private String mail;
  
      private int packFlg;
  
      public long getId() {
          return id;
      }
      public void setId(long id) {
          this.id = id;
      }
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
      public String getPerson() {
          return person;
      }
      public void setPerson(String person) {
          this.person = person;
      }
      public String getMail() {
          return mail;
      }
      public void setMail(String mail) {
          this.mail = mail;
      }
  
  
      @OneToMany
       @JoinColumn(name="customerId",referencedColumnName="customer_id")
      private List&lt;MainOrder&gt; mainOrder;
      public List&lt;MainOrder&gt; getMaiOrder() {
          return mainOrder;
      }
  
      public void setMaiOrder(List&lt;MainOrder&gt; mainOrder) {
          this.mainOrder = mainOrder;
      }
  
  
  }
  </code></pre>
  
  <p>-MainOrder.java</p>
  
  <pre><code>package com.mdr.model;
  
  import java.io.Serializable;
  
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
  import javax.persistence.ManyToOne;
  import javax.persistence.Table;
  
  @Entity
  @Table(name="main_order")
  public class MainOrder{
  
      @Id
      @GeneratedValue
      @Column(name="id")
      private long id;
      @Column(name="file_dir")
      private String fileDir;
      @Column(name="date")
      private String date;
      @Column(name="customer_id")
      private long customerId;
      @Column(name="quantity")
  
  
  
      /** getter and setter */
      public long getId() {
          return id;
      }
      public void setId(long id) {
          this.id = id;
      }
      public String getFileDir() {
          return fileDir;
      }
      public void setFileDir(String fileDir) {
          this.fileDir = fileDir;
      }
      public String getDate() {
          return date;
      }
      public void setDate(String date) {
          this.date = date;
      }
      public long getCustomerId() {
          return customerId;
      }
      public void setCustomerId(long customerId) {
          this.customerId = customerId;
      }
  
         @ManyToOne
      private Customer customer;
         public Customer getCustomer() {
           return customer;
       }
  
       public void setCustomer(Customer customer) {
              this.customer = customer;
       }
  
  }
  </code></pre>
  
  <p>I want query email from table customer. I try query like this</p>
  
  <pre><code>public String getCustomerEmail(){
          Session session = HibernateSessionFactory.getSession();
          //Session session = sessionFactory.openSession();
          String maxOrderNoDay = "";
          try{
              maxOrderNoDay = (String) session.createQuery("select mail from Customer cus join cus.MainOrder man WHERE man.id = 1").uniqueResult();
  
          }catch(HibernateException e){
              e.printStackTrace();
          }finally{
              session.close();
              return  maxOrderNoDay;
          }
      }
  </code></pre>
  
  <p>it always show error like this</p>
  
  <pre><code>    java.lang.NullPointerException
      at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1456)
      at org.hibernate.cfg.annotations.CollectionBinder.bindOneToManySecondPass(CollectionBinder.java:864)
      at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:779)
      at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:728)
      at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:70)
      at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1695)
      at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
      at com.mdr.dao.HibernateSessionFactory.&lt;clinit&gt;(HibernateSessionFactory.java:32)
      at com.mdr.dao.MainOrderDaoImpl.getCustomerEmail(MainOrderDaoImpl.java:104)
      at com.mdr.dao.TestDao.main(TestDao.java:138)
  %%%% Error Creating SessionFactory %%%%
  org.hibernate.DuplicateMappingException: Duplicate collection role mapping com.mdr.model.Customer.mainOrder
      at org.hibernate.cfg.Configuration$MappingsImpl.addCollection(Configuration.java:2859)
      at org.hibernate.cfg.annotations.CollectionBinder.bind(CollectionBinder.java:527)
      at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:2156)
      at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:963)
      at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:796)
      at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3788)
      at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3742)
      at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
      at com.mdr.dao.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:71)
      at com.mdr.dao.HibernateSessionFactory.getSession(HibernateSessionFactory.java:54)
      at com.mdr.dao.MainOrderDaoImpl.getCustomerEmail(MainOrderDaoImpl.java:104)
      at com.mdr.dao.TestDao.main(TestDao.java:138)
  Exception in thread "main" java.lang.NullPointerException
      at com.mdr.dao.MainOrderDaoImpl.getCustomerEmail(MainOrderDaoImpl.java:113)
      at com.mdr.dao.TestDao.main(TestDao.java:138)
  </code></pre>
  
  <p>-hibernate.cfg.xml</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  
  &lt;hibernate-configuration&gt;
  
      &lt;session-factory&gt;
          &lt;property name="javax.persistence.validation.mode"&gt;none&lt;/property&gt;
          &lt;!-- Database connection settings --&gt;
  
          &lt;!-- Database connection settings --&gt;
          &lt;property name="connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
  
          &lt;property name="connection.url"&gt;jdbc:mysql://localhost:3306/mydb&lt;/property&gt;
          &lt;property name="connection.username"&gt;root&lt;/property&gt;
          &lt;property name="connection.password"&gt;&lt;/property&gt;
  
          &lt;property name="c3p0.min_size"&gt;5&lt;/property&gt;
          &lt;property name="c3p0.max_size"&gt;20&lt;/property&gt;
          &lt;property name="c3p0.timeout"&gt;1800&lt;/property&gt;
          &lt;property name="c3p0.max_statements"&gt;50&lt;/property&gt;
  &lt;!--        
  
          &lt;!-- SQL dialect --&gt;
          &lt;property name="dialect"&gt;org.hibernate.dialect.MySQLInnoDBDialect&lt;/property&gt;
          &lt;!-- Enable Hibernate's current session context --&gt;
          &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;
  
          &lt;!-- Disable the second-level cache --&gt;
          &lt;property name="cache.provider_class"&gt;org.hibernate.cache.NoCacheProvider&lt;/property&gt;
  
          &lt;!-- Echo all executed SQL to stdout --&gt;
          &lt;property name="show_sql"&gt;true&lt;/property&gt;
  
          &lt;!-- Drop and re-create the database schema on startup --&gt;
          &lt;property name="hbm2ddl.auto"&gt;update&lt;/property&gt;
  
          &lt;property name="hibernate.connection.characterEncoding"&gt;UTF-8&lt;/property&gt;
  
  
  
          &lt;!-- Mapping (Map the Bean Class to Table in Database) --&gt;
          &lt;mapping class="com.mdr.model.Customer" /&gt;
          &lt;mapping class="com.mdr.model.MainOrder" /&gt;
  &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>Please help me!</p>