java - JPA - How can I cascade delete an entity that references itself? -


i have following entity:

@entity @table(name = "parameter_choice") @namedqueries({         @namedquery(name = "listparameterchoicesbyparameter",                 query = "select pc parameterchoice pc 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; 

then create entity second 1 id of first 1 parentparameterchoiceid. when try delete first entity, foreign key constraint error:

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) still referenced table "parameter_choice" 

as can see, tried orphanremoval = true, cascade = cascadetype.remove no result. there way cascade delete first entity instance sub-entities?

edit code creates entities:

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

and code delete entity:

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

as indicated jb nizet in comments, missing manytoone association seems necessary allow cascade deletion:

@manytoone(fetch = fetchtype.lazy, cascade = cascadetype.remove) @joincolumn(name = "parent_parameter_choice_id", insertable = false, updatable = false) private parameterchoice parentparameterchoice; 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -