gpt4 book ai didi

relational-database - 错误 OneTOMany/ManyToOne 映射 : null at time of saving parent - child table

转载 作者:行者123 更新时间:2023-12-04 14:50:13 25 4
gpt4 key购买 nike

我有两个实体:
我明白这个问题。 FK(名称:LOGGEMPLOYEEMODEL_ID)位于“loggingmodel”表中。
在为父表“employeedetails”保存新记录时,子表也应该更新。但我收到错误消息:“org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException:列“LOGGINGEMPLOYEEMODEL_ID”不允许为空;SQL 语句:
插入 LoggingModel (infotext, loggingEmployeeModel_id, title, id) 值 (?, ?, ?, ?) [23502-199]"
我明白了,对于子表的 FK,employeedetails 的新 ID 是未知的。 (如果它从 Postman 注入(inject)中取出“employeeModelLogging”部分,那么我不会收到约束错误,所以它应该是 loggingModel 部分)
我如何在 JPA - Hibernate 中解决这个问题?
父端:一对多:


@Entity
@AllArgsConstructor
@Getter
@Setter
@NoArgsConstructor
@Table(name="employeedetails", schema="public")
public class EmployeeModel implements Serializable {

private static final long serialVersionUID = -3009157732242241606L;

@Id
@Column(name="id")
@SequenceGenerator(initialValue=1, name="employeedetails_seq", sequenceName="employeedetails_sequence", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="employeedetails_seq")
private Long id;
...

@OneToMany(mappedBy = "loggingEmployeeModel",fetch=FetchType.LAZY , cascade = CascadeType.ALL)
private List<LoggingModel> employeeModelLogging;

...

}

子端:多对一:
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class LoggingModel {

@Id
@Column(name = "id")
@SequenceGenerator(initialValue = 1, name = "log_seq", sequenceName = "log_sequence", allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "log_seq")
private Long id;
@Column()
private String title;
@Column()
private String infotext;

@ManyToOne (cascade = CascadeType.ALL)
@JoinColumn(name = "loggingEmployeeModel_id", referencedColumnName = "id", nullable = false)
private EmployeeModel loggingEmployeeModel;

}
创建员工服务实现:

@Override
public ResponseEntity<Object> createEmployee(EmployeeModel employeeModelToCreate) {

if (employeeRepository.findByEmployeeName(employeeModelToCreate.getEmployeeName()).isEmpty()) {

employeeModelToCreate.setCreatedDateTime(LocalDateTime.now());
employeeModelToCreate.setModifiedDateTime(LocalDateTime.now());

Long unitId = employeeModelToCreate.getUnit().getId();
Unit unit = em.find(Unit.class, unitId);
employeeModelToCreate.setUnit(em.getReference(Unit.class, unitId)); // Alternative 2

EmployeeModel savedEmployeeModel = employeeRepository.save(employeeModelToCreate);

if (employeeRepository.findById(savedEmployeeModel .getId()).isPresent())
return ResponseEntity.ok("User Created Successfully");
else
return ResponseEntity.unprocessableEntity().body("Failed Creating User as Specified");
} else {
return ResponseEntity.unprocessableEntity().body("msg: Employee with this employee name: " + employeeModelToCreate.getEmployeeName() + ", already exist in DB!");
}
}

这就是我用 Postman 注入(inject)的内容:
{
"employeeName": "Nico",
"employeeCode": "ECN0004",
"designation": "ZZZZZ",
"address": {
"doorNumber": "37",
"street": "Laakse Laan",
"city": "Zutphen"
},
"department": {
"deptName": "Nieuwegein"
},
"employeeModelLogging":[
{
"title": "Dit is de titel - nieuw opmerking",
"infotext": "Dit is de infotext "
},
{
"title": "Dit is de titel van de 2e opmerking ",
"infotext": "Dit is de infotext van de 2e opmerking"
}
],
"unit": {
"id": 3
},
"roles": [
{
"id": 1,
"name":"Admin",
"description": "Administrator"
},
{
"id": 2,
"name":"Guest",
"description": "Gast"
}
]
}

有人可以提出想法/提示我做错了什么吗?

最佳答案

我认为模型类是从 JSON 直接映射到您的实体的,对吗?如果是这样,您将遇到仅在一侧设置的双向关系。 unmarshalling从 JSON 创建新 LoggingModel实例,并将它们放入一个列表中。然后在创建的 EmployeeModel 上设置该列表实例。 EmployeeModel实例现在可以引用它的所有 LoggingModel实例。仍然缺少的是 LoggingModel 中的引用EmployeeModel 的实例实例。 JSON unmarshalling没有设置它,因为它根本不知道它应该。
一个快速的解决方案是自己修复这些引用。快速 for-each 语句就足够了:

employeeModel.getEmployeeModelLogging().forEach(lm -> lm.setLoggingEmployeeModel(employeeModel);

关于relational-database - 错误 OneTOMany/ManyToOne 映射 : null at time of saving parent - child table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69217655/

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