gpt4 book ai didi

java - JPA - 如何级联删除引用自身的实体?

转载 作者:行者123 更新时间:2023-12-01 04:33:09 25 4
gpt4 key购买 nike

我有以下实体:

@Entity
@Table(name = "parameter_choice")
@NamedQueries({
@NamedQuery(name = "listParameterChoicesByParameter",
query = "SELECT pc FROM ParameterChoice pc WHERE pc.ParameterId = :parameterId")
})

@XmlRootElement
@Cacheable(false)
public class ParameterChoice implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ParameterChoiceSequenceGenerator")
@SequenceGenerator(allocationSize = 1, name = "ParameterChoiceSequenceGenerator", sequenceName = "parameter_choice_id_seq")
@Column(nullable = false)
private Integer id;

@Column(name = "parameter_id", nullable = false)
private Integer ParameterId;

@Column(name = "parent_parameter_choice_id", nullable = true)
private Integer parentParameterChoiceId;

@OneToMany(fetch = FetchType.LAZY, orphanRemoval = true, cascade = CascadeType.REMOVE)
@JoinColumn(name = "parent_parameter_choice_id", insertable = false, updatable = false)
private List<ParameterChoice> parameterChoices;

@Column(name = "canonical_name", nullable = false)
private String canonicalName;

@Column(name = "ui_only", nullable = false)
private Boolean uiOnly;

然后,我创建一个实体,然后创建第二个实体,其中第一个实体的 id 为 parentParameterChoiceId。当我尝试删除第一个实体时,出现外键约束错误:

org.postgresql.util.PSQLException: ERROR: update or delete on table "parameter_choice" violates foreign key constraint "fk_parameter_choice_parameter_choice" on table "parameter_choice" Detail: Key (id)=(15) is still referenced from table "parameter_choice"

如您所见,我尝试了orphanRemoval = true,cascade = CascadeType.REMOVE但没有结果。有没有办法级联删除第一个实体实例及其所有子实体?

编辑创建实体的代码:

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/parameter-choice")
public String addParameterChoice(ParameterChoice parameterChoice) {
String bodyContent = "";
String errMessage = "Error in POST: /parameter-choice/.";
try {
em.persist(parameterChoice);
bodyContent = mapper.writeValueAsString(parameterChoice);
return responseBuilder.buildString(bodyContent, ResponseBuilder.OK);
} catch (IOException e) {
logger.log(Level.SEVERE, errMessage, e);
return responseBuilder.buildString("errMessage", ResponseBuilder.ERROR);
}
}

以及删除实体的代码:

@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Path("/parameter-choice/{parameterChoiceId}")
public String deleteParameterChoice(@PathParam("parameterChoiceId") String parameterChoiceId) {
String bodyContent = "OK";
ParameterChoice parameterChoice = em.find(ParameterChoice.class, Integer.parseInt(parameterChoiceId));
em.remove(parameterChoice);
return responseBuilder.buildString(bodyContent, ResponseBuilder.OK);
}

最佳答案

正如 JB Nizet 在评论中指出的,我错过了 ManyToOne 关联,这似乎是允许级联删除所必需的:

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
@JoinColumn(name = "parent_parameter_choice_id", insertable = false, updatable = false)
private ParameterChoice parentParameterChoice;

关于java - JPA - 如何级联删除引用自身的实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17718141/

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