gpt4 book ai didi

mysql - JPA 和 Hibernate - 复合主键与外键

转载 作者:行者123 更新时间:2023-11-29 09:31:57 24 4
gpt4 key购买 nike

所以我有两个表,我想在 Spring 内存中进行查询。我已经成功地在名为 Medicine 的条目中对“Drugs”表进行了建模,但是现在需要对“drugInteraction”表进行建模 - 它将包含 drug_id(PK 在 Medicine 表中称为 id)和 drugInteraction 中的 drug_name 的组合表,作为组合主键。

Python 中使用的模式:

cursor.execute("CREATE TABLE drugs(id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), description TEXT, toxicity TEXT)")


cursor.execute("CREATE TABLE drugInteractions (drug_id INT NOT NULL, name VARCHAR(90), description TEXT, PRIMARY KEY(drug_id, name), FOREIGN KEY (drug_id) REFERENCES drugs (id))")

drugInteractions 表的一些示例数据:


drug_id name description

1 "Abciximab" "The risk or severity of bleeding can be increased when Abciximab is combined with Lepirudin."
1 "Aceclofenac" "The risk or severity of bleeding and hemorrhage can be increased when Aceclofenac is combined with Lepirudin."
1. "Acemetacin" "The risk or severity of bleeding and hemorrhage can be increased when Lepirudin is combined with Acemetacin."

药物表的一些示例数据:

id.       name.       description

1 "Lepirudin" "Lepirudin is identical to...."
2 "Cetuximab" "Cetuximab is an epidermal growth..."

这是我为 Medicine.java 准备的内容:

package com.example.configbackendspring;

import net.minidev.json.JSONObject;

import javax.persistence.*;

@Entity
@Table(name = "drugs")
public class Medicine {
@Id
@GeneratedValue
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "description")
private String description;
@Column(name = "toxicity")
private String toxicity;

public Medicine(int id, String name, String description, String toxicity) {
this.id=id;
this.name=name;
this.description=description;
this.toxicity=toxicity;
}


public Medicine(){}


public int getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getToxicity() {
return toxicity;
}

public void setToxicity(String toxicity) {
this.toxicity = toxicity;
}

public JSONObject toJSONObject(){
JSONObject object = new JSONObject();
JSONObject medicineObject = new JSONObject();
medicineObject.appendField("name", this.name);
medicineObject.appendField("description", this.description);
medicineObject.appendField("toxicity", this.toxicity);
medicineObject.appendField("id", this.id);
object.appendField("medicine", medicineObject);
return object;

}
}

这就是我为 drugInteraction.java 所做的...这不起作用

package com.example.configbackendspring;

import net.minidev.json.JSONObject;

import javax.persistence.*;
import javax.resource.cci.Interaction;
import java.io.Serializable;


@Entity
@Table(name = "drugInteractions")
public class DrugInteraction {


@EmbeddedId
private InteractionId interactionId;

@Column(name = "description")
private String description;


public DrugInteraction(int drug_id, String name, String description) {
this.interactionId.drug_name = name;
this.interactionId.drug_id = drug_id;
this.description=description;

}


public DrugInteraction(){}


public Integer getId() {
return interactionId.drug_id;
}


public String getName() {
return interactionId.drug_name;
}


public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}


public JSONObject toJSONObject(){
JSONObject object = new JSONObject();
JSONObject interactionObject = new JSONObject();
interactionObject.appendField("name", interactionId.drug_name);
interactionObject.appendField("description", this.description);
interactionObject.appendField("drug_id", interactionId.drug_id);
object.appendField("drugInteraction", interactionObject);
return object;

}
}

这是InteractionId.java

package com.example.configbackendspring;

import lombok.*;

import java.io.Serializable;

@RequiredArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
@EqualsAndHashCode
public class InteractionId implements Serializable
{

// public InteractionId(int drug_id, String drug_name){
// this.drug_id=drug_id;
// this.drug_name=drug_name;
//
// }


@NonNull
public int drug_id;

@NonNull
public String drug_name;
}


我当前的问题是我不知道如何将药物中的外键 ID 与复合键链接起来。上面的代码正在编译,但数据库是空的,因此导入一定在某个地方失败了

请问有人可以建议我如何更改文件以模拟上述行为吗?我无法弄清楚如何使用外键对复合 ID 进行建模

最佳答案

你有很多错误。如果您使用@EmbeddedId,那么您的 ID 类需要是可嵌入的。

@Embeddable
public class InteractionId {
@Column(name="name")
String name;
Long drugId; //type should be same as for ID field on Medicine

//equals and hashcode etc.
}

您还需要从 DrugInteractionMedicine 的关系,并用 MapsId 进行注释:

@Entity
@Table(name = "drugInteractions")
public class DrugInteraction {

@EmbeddedId
private InteractionId interactionId;

@MapsId("drugId")//value corresponds to property in the ID class
@ManyToOne
@JoinColumn(name = "drug_id")
private Medicine medicine;
}

保存新实例:

DrugInteraction di = new DrugInteraction();
Medicine medicine = //an existing medicine
di.setName("Some Name");
di.setMedicine(medicine);
//save

作为替代方案,也可以使用 IDClass 而不是 EmbeddedId 来完成此操作:

//not an embeddable
public class InteractionId {
String name;
Long drugId; //type should be same as for ID field on Medicine

//equals and hashcode etc.
}

并更改映射:

@Entity
@Table(name = "drugInteractions")
@IdClass(InteractionId.class) //specify the ID class
public class DrugInteraction {

@Id
private String name;

@Id
@ManyToOne
@JoinColumn(name = "drug_id")
private Medicine medicine;
}

关于mysql - JPA 和 Hibernate - 复合主键与外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58527109/

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