jpa 2.0 - Update collection when remove the same entity from other collection -
i have 4 classes entities: ctpersonafirma, ctfirmadocumento , ctcontrato. person may sign several contracts , contract can signed multiple people.
ctpersonafirma: contains persons authorized sign.
ctcontrato: contains contracts
ctfirmadocumento: relationship between 2 tables above.
when delete person ctpersonafirma table cascading deletes ctfirmadocumento table there reference person removed, in collection ctfirmadocumentocollection of entity ctcontrato not updated.
public class ctcontrato implements serializable { @id @basic(optional = false) @notnull @column(name = "id_contrato") private integer idcontrato; @onetomany(cascade = cascadetype.all, mappedby = "ctcontrato", fetch=fetchtype.lazy) private collection<ctfirmadocumento> ctfirmadocumentocollection; public class ctpersonafirma implements serializable { @id @basic(optional = false) @notnull @column(name = "id_persona") private integer idpersona; @onetomany(cascade = cascadetype.all, mappedby = "ctpersonafirma") private collection<ctfirmadocumento> ctfirmadocumentocollection; public class ctfirmadocumento implements serializable { @embeddedid protected ctfirmadocumentopk ctfirmadocumentopk; @joincolumn(name = "id_persona_ref", referencedcolumnname = "id_persona", insertable = false, updatable = false) @manytoone(optional = false) private ctpersonafirma ctpersonafirma; @joincolumn(name = "id_contrato", referencedcolumnname = "id_contrato", insertable = false, updatable = false) @manytoone(optional = false) private ctcontrato ctcontrato;
jpa requires application maintain both sides of bidirectional relationships. when make changes 1 side, jpa not maintain other side you, , cached entities out of synch in database. depending on side change, changes might not persisted in database either.
in case, when remove ctfirmadocumento objects, should remove references objects in ctcontrato might reference it.
an alternative in case force refresh of ctcontrato objects after changes have been flushed or commited, better fix references directly avoid problems relationship later.
Comments
Post a Comment