Is it bad to have recursiveness in the code, but not in the serialization?

Viewed 73

I have this relation between two models: Formulario and Secao. Formulario has Secao on it, but is transient. Just for code management purposes. Secao has Formulario, but they have a JPA relation, being @ManyToOne.

To create a "Secao" i have to create one "Formulario" before and then set "Formulario" into the "Secao"(because after being saved in the database, the object has now an id).

The major problem is, now i have some sort of "circular" code. Formulario>Secao>Formulario>Secao (and go on). As soon i serialize the object it gets messy because of the recursivity. Using @JsonBackReference and @JsonManagedReference managed to fix the serialization problem, but i still have the objects recursive in the memory.

Is it horrible problem?

(This model/relation wasn't done by me and i can't remake it).

@Entity(name = "formularios")
@Where(clause = "deletado = 'false'")
public class Formulario extends EntidadeGenerica {

    ....
    @Transient
    @JsonManagedReference
    private List<Secao> secoes;

    @Transient
    private List<Dependencia> dependencias;

@Entity(name = "formulario_secoes")
@Where(clause = "deletado = 'false'")
public class Secao extends EntidadeGenerica {

    ....
    @Column(nullable = false)
    private Boolean visivelMobile;

    @Column(nullable = false, name = "ordem_exibicao")
    private Integer ordemExibicao;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "formulario_id")
    @JsonBackReference
    private Formulario formulario;

public Formulario save(@RequestBody Formulario objeto, HttpServletRequest request) {

    try {

        formularioRepository.save(objeto);
        List<Secao> novasSecoes = objeto.getSecoes();

        novasSecoes.stream().map(s -> {
            s.setFormulario(objeto);
            return secaoRepository.save(s);
        });

        return objeto;

    } catch (Exception e) {
        System.out.println(e.getMessage());
        return null;
    }

}
{
    "id": 52,
    "dataUltimaAtualizacao": "2020-03-02",
    "dataCadastro": "2020-02-28",
    "deletado": false,
    "cliente": 1,
    "modulo": 35,
    "nome": "TESTE123",
    "entidadeDestino": "teste123",
    "bancoDadosDestino": "palotina",
    "schemaDestino": "cadastro_tecnico_multifinalitario",
    "ordem": null,
    "formularioPaiId": null,
    "secoes": [
        {
            "id": null,
            "dataUltimaAtualizacao": null,
            "dataCadastro": "2020-02-28",
            "deletado": false,
            "nome": "Dados Morador",
            "visivelMobile": true,
            "ordemExibicao": 1,
            "campos": [
                {
                    "id": null,
                    "dataUltimaAtualizacao": null,
                    "dataCadastro": "2020-02-28",
                    "deletado": false,
                    "nome": "Nome",
                    "tipo": "TEXTO",
                    "ordemExibicao": 1,
                    "visivelMobile": true,
                    "abrirFormularioFilho": null,
                    "inativo": null,
                    "obrigatorio": true,
                    "secao": null,
                    "atributoDestino": "proprietario",
                    "dependencia": null,
                    "campoOpcaos": null
                }
            ]
        }
    ],
    "dependencias": null
}

enter image description here

0 Answers
Related