我的MDFe实体更新出现问题,在解释发生了什么之前,它们看起来像下图,我可以解释一下。
我们可以看到MDFe与这个MDFeDocumento的比率为1比多,MDFeDocumento与MDFeUnidadeTransporte的比率为1比多,这与MDFeUnidadeCarga的比率为1比。
CRUD此结构在整个MDFe上执行。编辑mdfe并更新实体时,mdfeDocumento会更改,但是,从那里,其他实体不会受到更改的影响。例如,如果在编辑MDFe并更改给定文档的传输单元时,在对MDFe实体进行更新时,传输单元不会受到修改。
如何进行这些插入/更改只能保存MDFe?如果你不能只插入/更新父实体,这是插入/更新另一个实体的最佳方式吗?
谢谢大家,如果对我的解释有任何疑问,我会改进。
抱歉我的英语不好,我是巴西人
字体代码:
@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;
更新方法是:
@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;
}
如果我有时间,我会举例说明你。
答案 0 :(得分:1)
你应该看一下@OneToMany标签中的cascade属性,应该设置为CascadeType.ALL,你也可能需要orphanRemoval = true 发布相关代码以及您尝试过的内容。