JPA failed to initialize a collection of role I can not solve

0

Well, I'm trying to make a select and I'm just making this mistake, I know what the error means, but these classes are not god's, and FK goes there and then it's in an infinite loop, I do not know what else is missing to work right or if you have a way to bypass this Select would be easier:

failed to lazily initialize a collection of role: br.glr.populisii.model.formulariodinamico.FormularDynamic.values, could not initialize proxy - no Session

My select today:

public CampoDoFormularioPreenchido obtemFormPreeCamPeloIdCampoEIdFormulario(Integer idCampo, Integer idFormulario) {

    //@formatter:off
    TypedQuery<CampoDoFormularioPreenchido> query = getEntityManager().createQuery(new StringBuilder()
        .append("SELECT campoPreenchido ") 
        .append("FROM CampoDoFormularioPreenchido campoPreenchido ") 
        .append("JOIN FETCH campoPreenchido.campo campo ") 
        .append("JOIN FETCH campoPreenchido.formularioPreenchido formularioPreenchido ")
        .append("JOIN FETCH campoPreenchido.formulario formulario ")
        .append("LEFT JOIN FETCH formulario.valores valores ")
        .append("LEFT JOIN FETCH valores.formularioDinamico formularioDinamico2 ")
        .append("LEFT JOIN FETCH formularioDinamico2.valores valores2 ")
        .append("WHERE campo.idCampo = :idCampo ")
        .append("AND formulario.id = :idFormulario")
        .toString(), CampoDoFormularioPreenchido.class);
    //@formatter:on

    try {

        CampoDoFormularioPreenchido campoDoFormularioPreenchido = query.setParameter("idCampo", idCampo)
                                                                       .setParameter("idFormulario", idFormulario)
                                                                       .getSingleResult();

        return campoDoFormularioPreenchido;

    } catch (NoResultException e) {
        return null;
    }
}

Classes involved:

@Entity
@Table(name = "FormPreeCam")
public class CampoDoFormularioPreenchido implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private CampoDoFormularioPreenchidoPK id = new CampoDoFormularioPreenchidoPK();

    @NotNull
    @Column(name = "AtrIdFormulario", nullable = false)
    private Integer idFormulario;

    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "AtrIdCampo", updatable = false, insertable = false)
    @MapsId("idCampo")
    private CampoDoFormularioDinamico campo;

    @Column(name = "AtrString", length = 40)
    private String valorString;

    @Column(name = "AtrTexto", length = 40, columnDefinition = "TEXT")
    private String valorTexto;

    @Temporal(TemporalType.DATE)
    @Column(name = "AtrData")
    private Date valorData;

    @Column(name = "AtrInteiro", length = 4, precision = 10)
    private Integer valorInteiro;

    @Column(name = "AtrNumerico", length = 9, precision = 14, scale = 2)
    private Double valorNumerico;

    @NotNull
    @Column(name = "AtrDataLancamento", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date dataLancamento;

    @Column(name = "AtrIdValorRestrito", length = 4, precision = 10)
    private Integer atrIdValorRestrito;

    // Relacionamentos
    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "AtrIdFormularioPreenchido", updatable = false, insertable = false)
    @MapsId("idFormularioPreenchido")
    private FormularioPreenchido formularioPreenchido;

    @JsonManagedReference
    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "AtrIdFormulario", insertable = false, updatable = false)
    private FormularioDinamico formulario;

    @ManyToOne(optional = true)
    @JoinColumn(name = "AtrIdValorRestrito", updatable = false, insertable = false)
    private FormDinCamVal valorRestrito;

    @PrePersist
    public void setaDefaults() {
        if (dataLancamento == null) {
            dataLancamento = new Date();
        }
    }

@Entity
@Table(name = "FORMDINCAM")
public class CampoDoFormularioDinamico implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "sequence_ger")
    @GenericGenerator(name = "sequence_ger", strategy = "br.com.glr.populisii.utils.GeradorSequencia", parameters = {
            @Parameter(name = "table_name", value = "SEQ"),
            @Parameter(name = "value_column_name", value = "AtrUltimoNumeroSequencia"),
            @Parameter(name = "segment_column_name", value = "AtrNomeTabela"),
            @Parameter(name = "segment_value", value = "FORMDINCAM") }) 
    @Column(name = "AtrIdCampo", length = 4, precision = 10)
    private Integer idCampo;

    @JsonManagedReference
    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "AtrIdFormulario", referencedColumnName = "AtrIdFormulario", updatable = false, insertable = false)
    private FormularioDinamico formulario;

    @Column(name = "AtrIdFormulario", length = 4, precision = 10)
    private Integer idFormulario;

    @NotNull
    @Column(name = "AtrNomeCampo", nullable = false, length = 40)
    private String nomeCampo;

    @NotNull
    @Enumerated(EnumType.STRING)
    @Column(name = "AtrTipoCampo", length = 1, nullable = false)
    private DomTipoCampo tipoCampo;

    @NotNull
    @Column(name = "AtrObrigatorio", length = 1, nullable = false)
    @Enumerated(EnumType.STRING)
    private DomSimNao preenchimentoObrigatorio;

    @NotNull
    @Column(name = "AtrAba", length = 2, precision = 5, nullable = false)
    private Integer aba;

    @NotNull
    @Column(name = "AtrPosicao", length = 2, precision = 5, nullable = false)
    private Integer posicao;

    @Column(name = "AtrTamanhoArquivo", length = 2, precision = 5)
    private Integer tamanhoMaximoDoArquivo;

    @Enumerated(EnumType.STRING)
    @Column(name = "AtrTipoDigitacao", length = 1)
    private DomTipoDigitacao tipoDigitacao;

@Entity
@Table(name = "FORMPREE")
public class FormularioPreenchido implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "sequence_ger")
    @GenericGenerator(name = "sequence_ger", strategy = "br.com.glr.populisii.utils.GeradorSequencia", parameters = {
            @Parameter(name = "table_name", value = "SEQ"),
            @Parameter(name = "value_column_name", value = "AtrUltimoNumeroSequencia"),
            @Parameter(name = "segment_column_name", value = "AtrNomeTabela"),
            @Parameter(name = "segment_value", value = "FORMPREE") })
    @Column(name = "AtrIdFormularioPreenchido", length = 4, precision = 10)
    private Integer id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "AtrIdFormulario", referencedColumnName = "AtrIdFormulario", insertable = false, updatable = false)
    private FormularioDinamico formulario;

    @JsonBackReference
    @OneToMany(mappedBy = "formularioPreenchido")
    private Set<CampoDoFormularioPreenchido> campos;

    @NotNull
    @Column(name = "AtrIdFormulario", nullable = false)
    private Integer idFormulario;

    @NotNull
    @Column(name = "AtrIdGenerico", nullable = false, length = 4, precision = 10)
    private Integer idGenerico;

    @AnoMes
    @Column(name = "AtrAnoMesInicio", precision = 6, length = 5)
    private Integer anoMesInicio;

    @AnoMes
    @Column(name = "AtrAnoMesFinal", precision = 6, length = 5)
    private Integer anoMesFinal;

    @Column(name = "AtrDescricaoFormulario", length = 40)
    private String descricaoFormulario;

    @NotNull
    @Column(name = "AtrDataLancamento", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date dataLancamento;

    @PrePersist
    public void setaDefaults() {
        if (dataLancamento == null) {
            dataLancamento = new Date();
        }
    }

@Entity
@Table(name = "FORMDIN")
public class FormularioDinamico implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "sequence_ger")
    @GenericGenerator(name = "sequence_ger", strategy = "br.com.glr.populisii.utils.GeradorSequencia", parameters = {
            @Parameter(name = "table_name", value = "SEQ"),
            @Parameter(name = "value_column_name", value = "AtrUltimoNumeroSequencia"),
            @Parameter(name = "segment_column_name", value = "AtrNomeTabela"),
            @Parameter(name = "segment_value", value = "FORMDIN") })
    @Column(name = "AtrIdFormulario", length = 4, precision = 10)
    private Integer id;

    @NotNull
    @Column(name = "AtrNomeTabela", length = 40, nullable = false)
    private String nomeTabela;

    @NotNull
    @Column(name = "AtrNomeFormulario", length = 40, nullable = false)
    private String nomeFormulario;

    @NotNull
    @Column(name = "AtrHistorico", length = 1, nullable = false)
    @Enumerated(EnumType.STRING)
    private DomSimNao guardaHistorico;

    @NotNull
    @Column(name = "AtrLancamentoMultiplo", length = 1, nullable = false)
    @Enumerated(EnumType.STRING)
    private DomSimNao aceitaLancamentoMultiplo;

    @NotNull
    @Column(name = "atrQuantAbas", precision = 10, length = 4)
    private Integer quantidadeDeAbas;

    @NotNull
    @Column(name = "AtrAtivoInativo", length = 1, nullable = false)
    @Enumerated(EnumType.STRING)
    private DomAtivoInativo situacao;

    @JsonBackReference
    @OneToMany(mappedBy = "formulario")
    private Set<CampoDoFormularioDinamico> campos;

    @OneToMany(mappedBy = "formularioDinamico")
    private Set<FormDinCamVal> valores;
    
asked by anonymous 06.09.2018 / 20:50

0 answers