Save entity and all its daughter entities

Asked

Viewed 314 times

1

I have problems with the updates of my entity Mdfe, before explaining what happens, I would like you to look at the image below, from it I can explain better.

inserir a descrição da imagem aqui

We can verify that The Mdfe has a one-to-many relationship with Mdfedocumento, which also has a one-to-many relationship with Mdfeunidadetransport and this has a one-to-many relationship with Mdfeunidadecargo.

The CRUD of this structure is realized all over the Mdfe. When editing a document and updating the entity, the document is changed, however, from there, the other entities do not suffer the changes. For example: If when editing a Mdfe and changing the transport unit of a particular document, when updating the entity Mdfe, the transport unit does not undergo the change made.

How can I get these inserts/changes to be made only by saving Mdfe? If it is not possible to insert/update only the parent entity, which is the best way to insert/update the others?

Thank you all, and if you have any doubts in my explanation comment that I am improving.

1 answer

0

Gilvan if you are using JPA is easy, just use the annotation

cascadeType.ALL

in the list of your entities. For example:

The Mdfe class probably has a list of Mdfedocumento, in this list put the annotation above.

public class MDFe{

   @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
   protected List<MDFeDocumento> userAddresses;

}

or on Fetchtype put Ager

public class MDFe{

   @OneToMany(fetch = FetchType.EAGER)
   protected List<MDFeDocumento> userAddresses;

}

So on, your Mdfedocumento class should have a list of Mdfeunidadetransport with this annotation and this extends as far as you want. When saving Mdfe all the rest will be saved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.