gpt4 book ai didi

java - 为什么这个 Hibernate 关系不起作用?

转载 作者:行者123 更新时间:2023-11-30 05:35:00 26 4
gpt4 key购买 nike

我尝试以 n:m 关系连接披萨和配料,而所有披萨都将配料作为配料的属性列表。但是在关系表中,当我创建一个新的披萨并尝试提交时,关系表中的 PizzaID 出现错误。

关系表:

CREATE TABLE `Pizza_Ingredience_Relation` (
`PizzaID` int(11) NOT NULL,
`IngredientID` int(11) NOT NULL,
`Amount` int(11) NOT NULL,
`Volume_Unit` varchar(1) NOT NULL,
PRIMARY KEY (`PizzaID`,`IngredientID`),
KEY `FKc58en2gx5a8n1swmu9tda345` (`IngredientID`),
CONSTRAINT `FK_IngredienceId` FOREIGN KEY (`IngredientID`) REFERENCES `Zutatenliste` (`ID`),
CONSTRAINT `FKc58en2gx5a8n1swmu9tda345` FOREIGN KEY (`IngredientID`) REFERENCES `Zutatenliste` (`ID`),
CONSTRAINT `FKhghfxg8raskdydyu8o8msxtfn` FOREIGN KEY (`PizzaID`) REFERENCES `Pizza` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

成分表:

CREATE TABLE `Zutatenliste` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(20) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

披萨 table :

CREATE TABLE `Pizza` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(20) NOT NULL,
`PreisKlein` double NOT NULL,
`PreisMittel` double NOT NULL,
`PreisGroß` double NOT NULL,
`PreisFamilie` double NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `ID` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

我有两个 hibernate 实体,一个是披萨实体,一个是成分实体:


package Model.PizzenDB.SQLConnectionClasses.MySQL;

import Model.PizzenDB.Pizza;
import org.hibernate.annotations.CollectionId;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.util.LinkedList;
import java.util.Set;

@Entity
@Table(name = "Pizza")
public class MySQLPizzaHibernateEntity {
@Id
@Column(name = "ID")
private int id;
@Column(name = "Name")
private String name;
@Column(name = "PreisKlein")
private double smallPrice;
@Column(name = "PreisMittel")
private double middlePrice;
@Column(name = "PreisGroß")
private double bigPrice;
@Column(name = "PreisFamilie")
private double familyPrice;
@ManyToMany(cascade = { CascadeType.ALL })
@JoinTable(
name = "Pizza_Ingredience_Relation",
joinColumns = { @JoinColumn(name = "PizzaID", referencedColumnName = "ID") },
inverseJoinColumns = { @JoinColumn(name = "IngredientID") }
)
private Set<MySQLIngredientWithAmountHibernateEntity> ingredience;

public MySQLPizzaHibernateEntity(String name, double smallPrice, double middlePrice, double bigPrice, double familyPrice) {
this.name = name;
this.smallPrice = smallPrice;
this.middlePrice = middlePrice;
this.bigPrice = bigPrice;
this.familyPrice = familyPrice;
}

public MySQLPizzaHibernateEntity() {
}



}

@Entity
@Table(name = "Zutatenliste")
@SecondaryTable(name = "Pizza_Ingredience_Relation", pkJoinColumns = @PrimaryKeyJoinColumn(name = "IngredientID", referencedColumnName = "ID"))
public class MySQLIngredientWithAmountHibernateEntity {

@Id
@Column(name = "ID")
private int id;
@Column(name = "Name")
private String name;
@Column(table = "Pizza_Ingredience_Relation", name="Amount")
private int amount;
@Column(table = "Pizza_Ingredience_Relation", name = "Volume_Unit")
private char unit;

public MySQLIngredientWithAmountHibernateEntity(String name) {
this.name = name;
}

public MySQLIngredientWithAmountHibernateEntity() {
this("");
}
}

我收到以下错误消息:

20:41:45 [main] [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] ERROR - Field 'PizzaID' doesn't have a default value
20:41:45 [main] [org.hibernate.internal.ExceptionMapperStandardImpl] ERROR - HHH000346: Error during managed flush [org.hibernate.exception.GenericJDBCException: could not execute statement]

我不确定具体出了什么问题,我猜它与 PizzaID 外键有关并且设置不正确。

最佳答案

对于多对多关系,您正在使用带有额外列的中间表,并且您需要嵌入键来包含披萨和成分对象(名称缩写)。类似于:

@Embeddable
public class PizzaIngredientPk {
private MySQLPizzaHibernateEntity pizza;
private MySQLIngredientWithAmountHibernateEntity ingredient;

@ManyToOne
public MySQLPizzaHibernateEntity getPizza() {
return pizza;
}
public void setPizza(MySQLPizzaHibernateEntity pizza) {
this.pizza = pizza;
}

@ManyToOne
public MySQLIngredientWithAmountHibernateEntity getIngredient() {
return ingredient;
}
public void setIngredientID(MySQLIngredientWithAmountHibernateEntity ingredient) {
this.ingredient = ingredient;
}
}

然后这将充当 MySQLIngredientWithAmountHibernateEntity 中的嵌入键,如

    @EmbeddedId
PizzaIngredientPk pk = new PizzaIngredientPk();

但这不适用于用于一对一关系的Secondarytable。 @SecondaryTable 要求映射具有主键,但在这种情况下嵌入式 ID 将成为 PK。事实上,你的设计有缺陷。您正在尝试将多对多关系的一侧变为一对一。

根据 JPA 文档,使用 EmbeddedId 注释时必须只有一个 EmbeddedId 注释,并且不能有 Id 注释。

关于java - 为什么这个 Hibernate 关系不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56827308/

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