[英]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. 我在更新我的MDFe实体时遇到问题,在解释发生什么之前,它们看起来像下面的图片,我可以从中进行解释。
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. 我们可以看到,与该MDFeDocumento相比,MDFe具有一对多的比例;与MDFeUnidadeTransporte相比,MDFe也具有一对多的比例;而与MDFeUnidadeCarga则具有一对多的比例。
The CRUD this structure is performed all over the MDFe. CRUD这种结构是在整个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. 编辑mdfe并更新实体时,会更改mdfeDocumento,但是从那里开始,其他实体不会受到更改的影响。 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. 例如,如果在编辑MDFe并更改给定文档的传输单位时,同时提供MDFe实体的更新,则传输单位不会受到修改。
How can I make these inserts / changes are made only saving the MDFe? 我该如何进行这些插入/仅保存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. 如果有时间,我会举一个实用的例子向您展示。
您应该查看@OneToMany标记中的cascade属性,应将其设置为CascadeType.ALL,也可能需要orphanRemoval = true发布相关代码以及您尝试过的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.