gpt4 book ai didi

java - Jackson - 双向 - 反序列化父 ID

转载 作者:行者123 更新时间:2023-12-02 08:42:34 25 4
gpt4 key购买 nike

当我具有双向关系时,我无法使用 json 文件填充实体。

父类:

@Entity
@Table(name = ".....")
public class CpoPipeline implements... {

private String pipelineId;
private String pipelineSource;
private String pipelineVersion;
private Set<CpoPipelineParameter> cpoPipelineParameters;

@OneToMany(fetch = FetchType.EAGER, mappedBy = "cpoPipeline")
public Set<CpoPipelineParameter> getCpoPipelineParameters() {
return this.cpoPipelineParameters;
}

子类:

@Entity
@Table(name = ".....")
public class CpoPipelineParameter implements ...{

private String pipelineParameterId;
private CpoPipeline cpoPipeline;
private String pipelineParameterName;
private String pipelineParameterValue;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "pipeline_id", nullable = false)
@JsonIgnore
public CpoPipeline getCpoPipeline() {
return this.cpoPipeline;
}

Json:

{
"cpoPipeline": {
"pipelineId": "1",
"pipelineSource": "C://",
"pipelineVersion": "2.0",
"cpoPipelineParameters": [
{
"pipelineParameterId": "2",
"pipelineParameterName": "Process_Group_Variable_Year",
"pipelineParameterValue": "2020"
},
{
"pipelineParameterId": "1",
"pipelineParameterName": "Process_Group_Variable_Directory",
"pipelineParameterValue": "C://"
}
]
}
}

当我尝试使用 json 文件填充实体时,父 ID 不会进入子实体。
因此,当我尝试坚持时,我收到错误:

17:26:54.714 [main] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - NULL not allowed for column "PIPELINE_ID";
SQL statement:insert into cpo_pipeline_parameter (pipeline_id, pipeline_parameter_name, pipeline_parameter_value, pipeline_parameter_id) values (?, ?, ?, ?)

最佳答案

您需要为每个CpoPipelineParameter设置CpoPipeline。在双向关系中,如果您想将父级与子级保存在一起,则必须为每个子级设置父级以使双方同步。

cpoPipeline.getCpoPipelineParameters().forEach(d -> d.setCpoPipeline(cpoPipeline));

关于java - Jackson - 双向 - 反序列化父 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61280816/

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