Gilvan André
Gilvan André

Reputation: 531

Save entity and all its child entities

I'm having problems with updates of my MDFe entity, before explaining what happens, they looked like the image below, from it I can explain.

enter image description here

We can see that the MDFe has a ratio of one to many with this MDFeDocumento which also has a ratio of one to many with MDFeUnidadeTransporte and this has a ratio of one to many with MDFeUnidadeCarga.

The CRUD this structure is performed all over the MDFe. When editing a mdfe and to update the entity, the mdfeDocumento is changed, however, from there, the other entities do not suffer from the changes. For example, if when editing a MDFe and change the transport unit of a given document, while giving update on MDFe entity, the transport unit does not suffer modification.

How can I make these inserts / changes are made only saving the MDFe? If you can not insert / update only the parent entity, which is the best way to insert / update the other?

Thank you all, and if in doubt to comment on my explanation that I will improve.

Sorry for my bad English, I am 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 I have time, I make an functional example to show you.

Upvotes: 0

Views: 126

Answers (1)

Eus777
Eus777

Reputation: 63

You should look at the cascade attribute in your @OneToMany tag, should be set to CascadeType.ALL, also you may need orphanRemoval = true Post the relevant code and what you have tried.

Upvotes: 1

Related Questions