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



我有以下实体:

@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;

最新更新