gpt4 book ai didi

java - 主键由一个外键组成

转载 作者:行者123 更新时间:2023-12-01 06:03:11 24 4
gpt4 key购买 nike

我正在使用 Hibernate 编写 JEE Web 应用程序。我正在使用注释在 beans 中进行映射。

我展示

enter image description here尽管我对论坛和其他网站进行了研究,但我无法使我的主键与外键组合,如上图所示。

我刚刚设法将我的“id-command”键放入 FK,但我希望她来编写 PK。

预先感谢您的帮助。

祝你有美好的一天。

最佳答案

感谢您的回答。

当应用程序首次启动时,数据库没有表。它们是由 Hibernate 创建的。

我给你我的代码(我给你的代码中没有 getter 和 setter):BonLivraison 类(该类代码末尾只是一个尝试)

包 sii.dsi.beans;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.*;

@SuppressWarnings("serial")
@Entity
@Table
public class BonLivraison implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id_bon_livraison;
@Column(nullable=false, length=255, unique=false)
private String numero_bon_livraison;
@Column(nullable=false, unique=false)
private double montant_bon_livraison;
@Column(nullable=false, unique=false)
private Date date_bon_livraison;
@Column(nullable=false, unique=false)
private Date date_depot_facture;

@OneToOne
@JoinColumn(name="id_commande")
private PosteLigne poste_ligne_id_commande;
@OneToOne
@JoinColumn(name="id_posteligne")
private PosteLigne poste_ligne_id;

// Getters and Setters
}

指挥官级:

package sii.dsi.beans;

import java.io.Serializable;

import javax.persistence.*;

import sii.dsi.beans.Affaire;

@SuppressWarnings("serial")
@Entity
@Table
public class Commande implements Serializable{
@Column
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id_commande;
@Column(nullable=false, length=255, unique=false)
private String numero_commande;
@Column(nullable=false, length=255, unique=false)
private String numero_bc;
@OneToOne
private Affaire affaire;

}

PosteLigne:

package sii.dsi.beans;

import java.io.Serializable;

import javax.persistence.*;

@SuppressWarnings("serial")
@Entity
@Table
public class PosteLigne implements Serializable{

@EmbeddedId
private PosteLignePK poste_ligne_pk;
@Id
private int id_poste_ligne;
@Column(nullable=false, unique=false)
private double montant_poste_ligne;
@Column(nullable=false, unique=false)
private boolean clos;
@Column(nullable=false, unique=false)
private int numero_ligne;

}

PosteLignePK:

package sii.dsi.beans;

import java.io.Serializable;

import javax.persistence.*;

@SuppressWarnings("serial")
@Embeddable
public class PosteLignePK implements Serializable{

private int id_poste_ligne;
@OneToOne
@JoinColumn(name="id_commande")
private Commande commande;


@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id_poste_ligne;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PosteLignePK other = (PosteLignePK) obj;
if (id_poste_ligne != other.id_poste_ligne)
return false;
return true;
}

}

错误消息:

A Foreign key refering sii.dsi.beans.PosteLigne from sii.dsi.beans.BonLivraison has the wrong number of column. should be 2

我认为问题出在 BonLivraison 类(class)的末尾。

感谢您的帮助

关于java - 主键由一个外键组成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52180504/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com