简体   繁体   English

保存实体及其所有子实体

[英]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.

相关问题 如何使用join fetch通过一个查询获取父实体及其子实体及其子实体 - How to fetch Parent Entity with its Child Entities and their Child Entities with one query with join fetch 是否可以从父抽象实体中获取所有子实体? - Is it possible to fetch all child entities from a parent abstract entity? 如何使用 Lombok 在 Spring/JPA/Hibernate 中获取带有所有子实体和子实体的父实体 - How to get parent entity with all child entities and child entities of children in Spring/JPA/Hibernate with Lombok 休眠:如何确保在父级的所有子实体都删除后不删除父级? - Hibernate: How to ensure that a parent is not deleted when all its child entities are? 如何使用 Spring JPA DATA 仅获取特定的子实体及其具有特定值的父实体 - How to fetch only the specific child entity along with its Parent entities with specific value using Spring JPA DATA OneToMany 子实体未保存在 save() 上 - OneToMany child entities not persisted on save() Hibernate Query - 不是另一个实体的子实体的实体 - Hibernate Query - Entities that are NOT a child entity of another entity Hibernate HQL:当且仅当所有子实体具有相同值的属性时,如何 select 父实体? - Hibernate HQL: How to select Parent entity if and only if ALL Child entities have a property with same value? 冬眠 当我调用save(entity)时,它将所有我的实体更改保存到数据库 - Nhibernate. When i call save(entity) it saves all my entities change to the DB JpaRepository 保存刷新所有实体 - JpaRepository save flushes all entities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM