创建子元素时的 JPA 映射异常
JPA Mapping exception when creating a child element
我有那些持久实体,它们 constructors/getters/setters:
@Entity
public class Budget {
@Id
@GeneratedValue
private Long budgetId;
private double totalAmount;
private BudgetStatus status;
private Instant created;
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "budgetLineId")
private List<BudgetLine> budgetLines = new ArrayList<>();
// constructors / getters / setters
}
我想与这个实体建立单向的一对多关系:
@Entity
@Immutable
public class BudgetLine {
@Id
@GeneratedValue
private Long budgetLineId;
private String description;
private int tooth;
// constructors / getters / setters
}
我想我已经正确注释了实体,但我遇到了这个异常:
Cannot add or update a child row: a foreign key constraint fails
('dentist-app'.'budgetline', CONSTRAINT 'FKn3er98a66as3vxuj1kij2q040'
FOREIGN KEY ('budgetLineId') REFERENCES 'Budget' ('budgetId'))
使用此代码:
private List<Budget> createBudgets(Patient patient, BudgetStatus budgetStatus) {
List<Budget> budgets = new ArrayList<Budget>();
Treatment treatment = new Treatment("Implant");
treatmentRepository.save(treatment);
for (int i = 0; i < 5; i++) {
List<BudgetLine> budgetLines = new ArrayList<>();
BudgetLine budgetLine = budgetLineRepository.save(new BudgetLine("tooth", 120));
budgetLines.add(budgetLine);
Budget budget = budgetRepository
.save(new Budget(10, 10, 300, "Comments", budgetStatus, patient, treatment, budgetLines));
budgets.add(budget);
}
patient.setBudgets(budgets);
patientRepository.save(patient);
return budgets;
}
我不明白为什么我在持久化 BudgetLine
实例时会收到此异常,因为 BudgetLine
table 不应该有任何外键限制 Budget
因为它是单向关系。知道发生了什么事吗?谢谢。
您的映射和 table 定义是错误的。 budgetLineId
列是 BudgetLine
的唯一标识符,并且是 auto-generated,不能是 budgetId
列的联接 column/foreign 键Budget
。您需要在 BudgetLine
table:
中增加一个 budgetId
列
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "budgetId")
private List<BudgetLine> budgetLines = new ArrayList<>();
另外,我不明白为什么你没有外键约束,保证你的数据在数据库中的一致性,只是因为关联是单向的。您仍然需要外键约束来确保 BudgetLine 不会指向不存在的预算。
我有那些持久实体,它们 constructors/getters/setters:
@Entity
public class Budget {
@Id
@GeneratedValue
private Long budgetId;
private double totalAmount;
private BudgetStatus status;
private Instant created;
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "budgetLineId")
private List<BudgetLine> budgetLines = new ArrayList<>();
// constructors / getters / setters
}
我想与这个实体建立单向的一对多关系:
@Entity
@Immutable
public class BudgetLine {
@Id
@GeneratedValue
private Long budgetLineId;
private String description;
private int tooth;
// constructors / getters / setters
}
我想我已经正确注释了实体,但我遇到了这个异常:
Cannot add or update a child row: a foreign key constraint fails ('dentist-app'.'budgetline', CONSTRAINT 'FKn3er98a66as3vxuj1kij2q040' FOREIGN KEY ('budgetLineId') REFERENCES 'Budget' ('budgetId'))
使用此代码:
private List<Budget> createBudgets(Patient patient, BudgetStatus budgetStatus) {
List<Budget> budgets = new ArrayList<Budget>();
Treatment treatment = new Treatment("Implant");
treatmentRepository.save(treatment);
for (int i = 0; i < 5; i++) {
List<BudgetLine> budgetLines = new ArrayList<>();
BudgetLine budgetLine = budgetLineRepository.save(new BudgetLine("tooth", 120));
budgetLines.add(budgetLine);
Budget budget = budgetRepository
.save(new Budget(10, 10, 300, "Comments", budgetStatus, patient, treatment, budgetLines));
budgets.add(budget);
}
patient.setBudgets(budgets);
patientRepository.save(patient);
return budgets;
}
我不明白为什么我在持久化 BudgetLine
实例时会收到此异常,因为 BudgetLine
table 不应该有任何外键限制 Budget
因为它是单向关系。知道发生了什么事吗?谢谢。
您的映射和 table 定义是错误的。 budgetLineId
列是 BudgetLine
的唯一标识符,并且是 auto-generated,不能是 budgetId
列的联接 column/foreign 键Budget
。您需要在 BudgetLine
table:
budgetId
列
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "budgetId")
private List<BudgetLine> budgetLines = new ArrayList<>();
另外,我不明白为什么你没有外键约束,保证你的数据在数据库中的一致性,只是因为关联是单向的。您仍然需要外键约束来确保 BudgetLine 不会指向不存在的预算。