title: composite key join columns
body: <p>My table VENDORBRANCH has <strong>composite keys</strong>: "vendorCode" and "vendorBranchCode" which I have defined using the @Id annotation and using @IdClass. The field "vendorCode" is referenced as a foreign key in VENDORCRTERMS class. I'm using postgresql db. 
  Right now my sql query in the service implimentation looks like this but i want to include composite keys in the query:</p>
  
  <pre><code> Query&lt;?&gt; query = session.createQuery("from VENDORBRANCH where vendorCode = ?");
          query.setParameter(0, mf01_vendorCode);
  </code></pre>
  
  <p>I'm very new to hibernate so tried a few options for the select query but I'm not sure if it's correct to do it this way. So, what would be the best select statement to use for a composite key??</p>
  
  <p><strong>VENDORBRANCH</strong> class:</p>
  
  <pre><code>import java.io.Serializable;
  import java.util.Date;    
  import javax.persistence.Embeddable;
  import javax.persistence.EmbeddedId;
  import javax.persistence.Entity;
  import javax.persistence.Id;
  import javax.persistence.IdClass;
  import javax.persistence.JoinColumn;
  import javax.persistence.ManyToOne;
  import com.parkson.poMain.backend.data.VENDORBRANCH.VBpk;
  
  @SuppressWarnings("serial")
  @Entity
  @IdClass(VBpk.class)
  public class VENDORBRANCH implements Serializable {
  
      @Id
      private String vendorCode;
  
      @Id
      private String vendorBranchCode;
  
      //getters and setters
  
      // inner class defined for primary key(composite keys)
      public static class VBpk implements Serializable {
          protected String vendorCode;
          protected String vendorBranchCode;
  
          public String getvendorCode() {
              return vendorCode;
          }
  
          public void vendorCode(String vendorCode) {
              this.vendorCode = vendorCode;
          }
  
          public String vendorBranchCode() {
              return vendorBranchCode;
          }
  
          public void vendorBranchCode(String vendorBranchCode) {
              this.vendorBranchCode = vendorBranchCode;
          }
  
          public VBpk(){}
  
          public VBpk(String vendorCode,String vendorBranchCode){
                  this.vendorCode = vendorCode;
                  this.vendorBranchCode = vendorBranchCode;
              }
  
          @Override
          public int hashCode() {
              final int prime = 31;
              int result = 1;
              result = prime * result + ((vendorBranchCode == null) ? 0 : vendorBranchCode.hashCode());
              result = prime * result + ((vendorCode == null) ? 0 : vendorCode.hashCode());
              return result;
          }
  
          @Override
          public boolean equals(Object obj) {
              if (this == obj)
                  return true;
              if (obj == null)
                  return false;
              if (getClass() != obj.getClass())
                  return false;
              VBpk other = (VBpk) obj;
              if (vendorBranchCode == null) {
                  if (other.vendorBranchCode != null)
                      return false;
              } else if (!vendorBranchCode.equals(other.vendorBranchCode))
                  return false;
              if (vendorCode == null) {
                  if (other.vendorCode != null)
                      return false;
              } else if (!vendorCode.equals(other.vendorCode))
                  return false;
              return true;
          }       
      }
  }
  </code></pre>
  
  <p><strong>My other class: VENDORCRTERMS</strong></p>
  
  <pre><code>import java.io.Serializable;
  import java.util.Date;    
  import javax.persistence.Entity;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.ManyToOne;
  
  @SuppressWarnings("serial")
  @Entity  
  public class VENDORCRTERMS implements Serializable {
  
      @Id
      private String  vcrId ;
  
     //This is the foreign key referenced from **VENDORBRANCH class** 
  @ManyToOne
  @JoinColumns( {
      @JoinColumn(name="vendorcode", nullable = false),
      @JoinColumn(name="vendorBranchCode", nullable = false)} )
      private VENDORBRANCH vendorbranch_vendorcode = new VENDORBRANCH();      
  
      // foreign key referenced from a different class
      @ManyToOne
      @JoinColumn(name= "creditterms_credittermscode" , nullable = false)
      private CREDITTERMS creditterms_credittermscode = new CREDITTERMS();
  
      //getters and setters
  
  }
  </code></pre>