Java 如何在Hibernate实体注释中将两个外键用作主键

Java 如何在Hibernate实体注释中将两个外键用作主键,java,mysql,hibernate,Java,Mysql,Hibernate,我想使用Hibernate实体注释从2个外键生成主键: ** 如何将这两个外键“comID”和“reference”设置为 带有Hibernate注释的LigneCommande表的主键!! 谢谢:) 我尝试了此代码,但不起作用: 类别“产品”: public class Produit implements Serializable{ @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @JoinTable

我想使用Hibernate实体注释从2个外键生成主键: **

  • 如何将这两个外键“comID”和“reference”设置为 带有Hibernate注释的LigneCommande表的主键!! 谢谢:)
我尝试了此代码,但不起作用:

类别“产品”:

public class Produit implements Serializable{

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "LigneCommande", catalog = "mkyongdb", joinColumns = { 
            @JoinColumn(name = "reference", nullable = false, updatable = false) }, 
            inverseJoinColumns = { @JoinColumn(name = "commande_id", 
                    nullable = false, updatable = false) })
    private List<Commande> commande;


    public List<Commande> getCommande() {
        return commande;
    }

    public void setCommande(List<Commande> commande) {
        this.commande = commande;
    }
}
@Entity
public class Commande implements Serializable{

@ManyToMany(fetch = FetchType.LAZY, mappedBy = "commande")
    private List<Produit> produit;

public List<Produit> getProduit() {
        return produit;
    }

    public void setProduit(List<Produit> produit) {
        this.produit = produit;
    }
}
公共类Produit实现可序列化{
@ManyToMany(fetch=FetchType.LAZY,cascade=CascadeType.ALL)
@JoinTable(name=“LigneCommande”,catalog=“mkyongdb”,joinColumns={
@JoinColumn(name=“reference”,nullable=false,updateable=false)},
inverseJoinColumns={@JoinColumn(name=“commande_id”,
nullable=false,updateable=false)})
私有列表命令;
公共列表getcommand(){
返回命令;
}
公共无效设置命令(列表命令){
this.commande=commande;
}
}
类“命令”:

public class Produit implements Serializable{

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "LigneCommande", catalog = "mkyongdb", joinColumns = { 
            @JoinColumn(name = "reference", nullable = false, updatable = false) }, 
            inverseJoinColumns = { @JoinColumn(name = "commande_id", 
                    nullable = false, updatable = false) })
    private List<Commande> commande;


    public List<Commande> getCommande() {
        return commande;
    }

    public void setCommande(List<Commande> commande) {
        this.commande = commande;
    }
}
@Entity
public class Commande implements Serializable{

@ManyToMany(fetch = FetchType.LAZY, mappedBy = "commande")
    private List<Produit> produit;

public List<Produit> getProduit() {
        return produit;
    }

    public void setProduit(List<Produit> produit) {
        this.produit = produit;
    }
}
@实体
公共类命令实现可序列化{
@ManyToMany(fetch=FetchType.LAZY,mappedBy=“commande”)
私人上市产品;
公共列表getProduit(){
返回产品;
}
公共无效setProduit(列表produit){
this.produit=produit;
}
}

除此之外,我没有任何异常或错误

这是解决方案:

public class LigneCommande implements Serializable {

@EmbeddedId
    protected LigneCommandePK ligneCommandePK;

    @Column(name = "quantite")
    private int quantite;

    @Column(name = "status")
    private String status;
    @JoinColumn(name = "produit_id", referencedColumnName = "id", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Produit produit;
    @JoinColumn(name = "commande_id", referencedColumnName = "id", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Commande commande;
}
这是一个“Produit”类:

@Entity
@Table(name = "produit")
public class Produit implements Serializable {@OneToMany(cascade = CascadeType.ALL, mappedBy = "produit")
    private Collection<LigneCommande> ligneCommandeCollection;
}
它是有效的,看看图片: