复合键联接列 [英] composite key join columns

查看:81
本文介绍了复合键联接列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My table VENDORBRANCH has composite keys: "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:

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

我刚进入休眠状态,因此尝试了一些选择查询选项,但是我不确定这样做是否正确.那么,用于复合键的最佳选择语句是什么?

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??

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

}

推荐答案

VENDORBRANCH定义了复合主键,但是在VENDORCRTERMS中,您仅在@JoinColumn上用作引用.这就是您的情况下的映射外观:

VENDORBRANCH has defined a composite primary key but in VENDORCRTERMS you only use on @JoinColumn for the reference. This is how the mapping should look like in your case:

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

这篇关于复合键联接列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆