hibernate - Save entity and all its child entities -
i'm having problems updates of mdfe entity, before explaining happens, looked image below, can explain.
we can see mdfe has ratio of 1 many mdfedocumento has ratio of 1 many mdfeunidadetransporte , has ratio of 1 many mdfeunidadecarga.
the crud structure performed on mdfe. when editing mdfe , update entity, mdfedocumento changed, however, there, other entities not suffer changes. example, if when editing mdfe , change transport unit of given document, while giving update on mdfe entity, transport unit not suffer modification.
how can make these inserts / changes made saving mdfe? if can not insert / update parent entity, best way insert / update other?
thank all, , if in doubt comment on explanation improve.
sorry bad english, brazilian
the font code:
@entity @table(name = "mdfe") public class mdfe implements ientity { private static final long serialversionuid = 1l; @id @generatedvalue(strategy = generationtype.auto) @column(name = "id") private long id; @enumerated(enumtype.ordinal) @column(name = "modelo", nullable = false) private modelodocenum modelo; @min(value = 0) @max(value = 999) @column(name = "serie", nullable = false) private integer serie; @min(value = 0) @column(name = "numero", nullable = false) private long numero; @lazycollection(lazycollectionoption.false) @onetomany(mappedby = "mdfe", orphanremoval = true, cascade = cascadetype.all) private list<mdfedocumento> mdfedocumentos; } @entity @table(name = "mdfe_documentos") public class mdfedocumento implements ientity { private static final long serialversionuid = 1l; @id @generatedvalue(strategy = generationtype.auto) @column(name = "id") private long id; @size(max = 44) @column(name = "chave", nullable = true, length = 50) private string chave; @notnull @manytoone @joincolumn(name = "mdfe_id", referencedcolumnname = "id", nullable = false) private mdfe mdfe; @lazycollection(lazycollectionoption.false) @onetomany(mappedby = "mdfedocumento", orphanremoval = true, cascade = cascadetype.all) private list<mdfeunidadetransporte> unidadestransporte; } public class mdfeunidadetransporte implements ientity { private static final long serialversionuid = 1l; @id @generatedvalue(strategy = generationtype.auto) @column(name = "id") private long id; @notnull @size(max = 20) @column(name = "ident_unid_transp", nullable = false, length = 20) private string identunidtransp; @notnull @enumerated(enumtype.ordinal) @column(name = "tipo_unid_transp", nullable = false) private tipounidadetransporteenum tipounidtransp; @min(0) @column(name = "quantidade_rateada", nullable = false, columndefinition = "decimal(15,2) default 0") private double quantidaderateada; @lazycollection(lazycollectionoption.false) @onetomany(mappedby = "unidadetransporte", orphanremoval = true, cascade = {cascadetype.all}) private list<mdfeunidadecarga> unidadescargas; @notnull @manytoone @joincolumn(name = "mdfe_documento_id", nullable = false, referencedcolumnname = "id") private mdfedocumento mdfedocumento; } @entity @table(name = "mdfe_unidades_cargas") public class mdfeunidadecarga implements ientity { private static final long serialversionuid = 1l; @id @generatedvalue(strategy = generationtype.auto) @column(name = "id") private long id; @notnull @size(max = 20) @column(name = "ident_unid_carga", nullable = false, length = 20) private string identunidcarga; @notnull @enumerated(enumtype.ordinal) @column(name = "tipo_unid_carga", nullable = false) private tipounidadecargaenum tipounidcarga; @min(0) @column(name = "quantidade_rateada", nullable = false, columndefinition = "decimal(15,2) default 0") private double quantidaderateada; @lazycollection(lazycollectionoption.false) @elementcollection @column(name = "num_lacre", length = 60, nullable = false) @collectiontable(name = "mdfe_lacres_cargas", joincolumns = @joincolumn(name = "mdfe_cargas_id")) private list<string> lacres; @notnull @manytoone @joincolumn(name = "mdfe_unidade_transporte_id", nullable = false, referencedcolumnname = "id") private mdfeunidadetransporte unidadetransporte;
the update metod is:
@override public t update(t entity) { entitymanager em = this.getentitymanager(); if (!em.gettransaction().isactive()) { em.gettransaction().begin(); } entity = em.merge(entity); em.gettransaction().commit(); return entity; }
if have time, make functional example show you.
you should @ cascade attribute in @onetomany tag, should set cascadetype.all, may need orphanremoval = true post relevant code , have tried.
Comments
Post a Comment