2017-05-25 40 views
2

我的表VENDORBRANCH有複合鍵:我使用@Id註釋和@IdClass定義的「vendorCode」和「vendorBranchCode」。 「vendorCode」字段在VENDORCRTERMS類中被引用爲外鍵。我正在使用postgresql數據庫。 眼下在服務執行力度我的SQL查詢看起來是這樣,但我想在查詢中包括組合鍵:複合鍵連接列

Query<?> query = session.createQuery("from VENDORBRANCH where vendorCode = ?"); 
     query.setParameter(0, mf01_vendorCode); 

我很新冬眠所以嘗試了幾種方案供選擇查詢,但我我不確定這樣做是否正確。那麼,什麼是用於複合鍵的最佳選擇語句?

VENDORBRANCH類:

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; 
     }  
    } 
} 

我的其他類:VENDORCRTERMS

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 

} 
+0

所以VENDORBRANCH有2個PK列,你定義了1個'JoinColumn' ......所以很難理解什麼? –

回答

3

VENDORBRANCH已經定義了一個複合主鍵,但是在VENDORCRTERMS中您只能用@JoinColumn作爲參考。這就是映射在你的情況下的外觀:

@ManyToOne 
@JoinColumns({ 
    @JoinColumn(name="vendorCode", referencedColumnName="vendorCode"), 
    @JoinColumn(name="vendorBranchCode", referencedColumnName="vendorBranchCode") 
}) 
private VENDORBRANCH vendorbranch_vendorcode 
0

的原因是:因爲他觀察到,有兩個@id S IN VENDORBRANCHVENDORCRTERMS類是迷茫。我有你的解決方案。如果將vendorCodevendorBranchCode設置爲​​以及只保留一個主鍵,該怎麼辦?

@Id 
private String vendorCode; 

我認爲這將滿足您的需求。