title: In hibernate how can write a query for retrieving data with a composite key?
body: <p>Here I have a table named receipt and its primary key is a composite key referred with other class <code>Receipt_compoundKey</code> with two variables named <code>voucher_id</code> and <code>company_id</code>, </p>
  
  <p>How I retrive data from receipt with condition like <code>company_id=1;</code></p>
  
  <h2>Receipt :</h2>
  
  <pre><code>@Entity
  
  @Table(name = AMAM_Constants.tb_name.RECEIPT_FROM, catalog = AMAM_Constants.db_name)
  public class Receipt implements Serializable {
  
  @Id
  private Receipt_CompoundKey id;
  @OneToOne
  @JoinColumn(name = "FROM_LEDGER")
  private Ledger fromLedger;
  @Column(name = "VOU_DATE")
  private Date voucher_Date;
  @Column(name = "TOTAL_AMOUNT")
  private double total_amount;
  @Column(name = "ACTIVE")
  private char active;
  @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  @JoinTable(name = AMAM_Constants.tb_name.RECEIPT_FROM_LINK_TO, joinColumns = {
      @JoinColumn(name = "COMPANY_ID"),
      @JoinColumn(name = "VOUCHER_ID")
  }, inverseJoinColumns = {
      @JoinColumn(name = "RECP_TO")})
  private List&lt;Receipt_To&gt; recptToList = new ArrayList&lt;Receipt_To&gt;();
  @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  @JoinTable(name = AMAM_Constants.tb_name.RECEIPT_FROM_LINK_ADJMTS, joinColumns = {
      @JoinColumn(name = "COMPANY_ID"),
      @JoinColumn(name = "VOUCHER_ID")
  }, inverseJoinColumns = {
      @JoinColumn(name = "RECPT_ADJS")})
  private List&lt;Receipt_Adj&gt; recptAdjments = new ArrayList&lt;Receipt_Adj&gt;();
  
  public List&lt;Receipt_Adj&gt; getRecptAdjments() {
      return recptAdjments;
  }
  
  public void setRecptAdjments(List&lt;Receipt_Adj&gt; recptAdjments) {
      this.recptAdjments = recptAdjments;
  }
  
  public List&lt;Receipt_To&gt; getRecptToList() {
      return recptToList;
  }
  
  public void setRecptToList(List&lt;Receipt_To&gt; recptToList) {
      this.recptToList = recptToList;
  }
  
  public char getActive() {
      return active;
  }
  
  public void setActive(char active) {
      this.active = active;
  }
  
  public double getTotal_amount() {
      return total_amount;
  }
  
  public void setTotal_amount(double total_amount) {
      this.total_amount = total_amount;
  }
  
  public Ledger getFromLedger() {
      return fromLedger;
  }
  
  public void setFromLedger(Ledger fromLedger) {
      this.fromLedger = fromLedger;
  }
  
  public Receipt_CompoundKey getId() {
      return id;
  }
  
  public void setId(Receipt_CompoundKey id) {
      this.id = id;
  }
  
  public Date getVoucher_Date() {
      return voucher_Date;
  }
  
  public void setVoucher_Date(Date voucher_Date) {
      this.voucher_Date = voucher_Date;
  }
  </code></pre>
  
  <p>}</p>
  
  <h2>Receipt_CompoundKey :</h2>
  
  <pre><code>@Embeddable
  public class Receipt_CompoundKey implements Serializable {
  
  @Column(name = "VOUCHER_ID")
  private long voucher_Id;
  @Column(name = "COMPANY_ID")
  private long company_Id;
  
  public Receipt_CompoundKey() {
  }
  
  public Receipt_CompoundKey(long voucher_Id) {
      this.voucher_Id = voucher_Id;
  }
  
  public Receipt_CompoundKey(long voucher_Id, long company_Id) {
      this.company_Id = company_Id;
      this.voucher_Id = voucher_Id;
  }
  
  public long getCompany_Id() {
      return company_Id;
  }
  
  public void setCompany_Id(long company_Id) {
      this.company_Id = company_Id;
  }
  
  public long getVoucher_Id() {
      return voucher_Id;
  }
  
  public void setVoucher_Id(long voucher_Id) {
      this.voucher_Id = voucher_Id;
  }
  </code></pre>
  
  <p>}</p>
  
  <h2>Query :</h2>
  
  <pre><code>String query = "from Receipt where active='Y' and  id=:id ";
  begin();
  objList = getSession().createQuery(query).setLong("id", key.getCompany_Id()).setLong("id", key.getVoucher_Id()).list();
  commit();
  </code></pre>