复合键连接列

时间:2017-05-25 10:51:14

标签: java hibernate jpa orm hibernate-mapping

我的桌子VENDORBRANCH有复合键:" vendorCode"和" vendorBranchCode"我使用@Id注释定义并使用@IdClass。字段" vendorCode"在VENDORCRTERMS类中引用为外键。我正在使用postgresql db。 现在我在服务实现中的sql查询看起来像这样,但我想在查询中包含复合键:

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

我对hibernate很新,所以尝试了一些选择查询的选项,但我不确定这样做是否正确。那么,用于复合键的最佳select语句是什么?

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

}

2 个答案:

答案 0 :(得分:3)

VENDORBRANCH已定义了一个复合主键,但在VENDORCRTERMS中,您只能在@JoinColumn上使用该参考。这就是映射在您的情况下的样子:

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

答案 1 :(得分:0)

原因是:VENDORCRTERMS类很困惑,因为他发现@id中有两个VENDORBRANCH。我有一个解决方案给你。如果您将vendorCodevendorBranchCode设为unique key并仅保留一个主键,该怎么办?

@Id
private String vendorCode;

我认为这将满足您的需求。