Save entity and all its child entities

1

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

We can verify that MDFe has a one-to-many relationship with MDF and Document whose it also has a one-to-many relationship with MDF and TransportUnit and it has a one to many relationship with MDF and LoadUp.

The CRUD of this structure is done all over the MDFe. When editing an mdfe and updating the entity, the mdfeDocument is changed, but from there, the other entities do not undergo changes. For example: When editing an MDFe and changing the transport unit of a given document, when the update is performed on the MDFe entity, the transport unit does not change.

How can I make these inserts / changes only be done by saving the MDFe? If it is not possible to insert / update only the parent entity, what is the best way to insert / update the others?

Thanks to all, and if you have any questions in my explanation, comment on what I'm getting better at.

    
asked by anonymous 29.04.2015 / 13:58

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 MDFeDocument list, in that list put the annotation quoted above.

public class MDFe{

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

}

or in FetchType put eager

public class MDFe{

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

}

so on, your MDFeDocument class must have a list of MDFeachUniversity with this annotation and it extends to wherever you want. When saving the MDFe all the rest will be saved.

    
08.01.2016 / 13:30