JPA / Hibernate - LazyLoading OneToMany duplicating records

1

Well, I have the following structure:

public class FinCxaTransacaoGrupo {

    @Id
    @GeneratedValue(generator="gen-uuid")
    @GenericGenerator(name="gen-uuid", strategy = "uuid2")
    @Type(type="pg-uuid")
    @Column(name="id_fin_cxa_transacao_grupo")
    private UUID idFinCxaTransacaoGrupo;

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="id_fin_cxa_transacao_destino")
    private FinCxaTransacao finCxaTransacaoDestino;

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="id_fin_cxa_transacao_origem")
    private FinCxaTransacao finCxaTransacaoOrigem;

}

public class FinCxaTransacao {

    @Id
    @GeneratedValue(generator = "gen-uuid")
    @GenericGenerator(name = "gen-uuid", strategy = "uuid2")
    @Type(type = "pg-uuid")
    @Column(name = "id_fin_cxa_transacao")
    private UUID idFinCxaTransacao;

    @OneToMany(cascade=CascadeType.ALL, mappedBy = "finCxaTransacao")
    private List<FinCxaPlanoLcto> finCxaPlanoLctos = new ArrayList<>();

    @OneToMany(mappedBy="finCxaTransacaoDestino")
    private List<FinCxaTransacaoGrupo> finCxaTransacaoGruposDestino = new ArrayList<>();

    @OneToMany(mappedBy="finCxaTransacaoOrigem")
    private List<FinCxaTransacaoGrupo> finCxaTransacaoGruposOrigem = new ArrayList<>();

}

public class FinCxaPlanoLcto {

    @Id
    @GeneratedValue(generator="gen-uuid")
    @GenericGenerator(name="gen-uuid", strategy = "uuid2")
    @Type(type="pg-uuid")
    @Column(name="id_fin_cxa_plano_lcto")
    private UUID idFinCxaPlanoLcto;

    @ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
    @JoinColumn(name="id_fin_cxa_transacao")
    private FinCxaTransacao finCxaTransacao;

}

Query executed to find the Group:

public FinCxaTransacaoGrupo buscarFinCxaTransacaoGrupoEstorno(UUID idFinCxaTransacao) {

    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<FinCxaTransacaoGrupo> query = cb.createQuery(FinCxaTransacaoGrupo.class);
    Root<FinCxaTransacaoGrupo> finCxaTransacaoGrupo = query.from(FinCxaTransacaoGrupo.class);
    query.where(cb.or(cb.equal(finCxaTransacaoGrupo.get(FinCxaTransacaoGrupo_.finCxaTransacaoDestino).get(
            FinCxaTransacao_.idFinCxaTransacao), idFinCxaTransacao),
            cb.equal(finCxaTransacaoGrupo.get(FinCxaTransacaoGrupo_.finCxaTransacaoOrigem).get(
                    FinCxaTransacao_.idFinCxaTransacao), idFinCxaTransacao)));

    query.distinct(true);

    query.select(finCxaTransacaoGrupo).distinct(true);
    return em.createQuery(query).getSingleResult();
}

The FinCxaTranslationGroup table contains a record. The table FinCxaTransacao contains a record for finalTranslationTest and finalTranslationType;

the FinChapterLine table contains two records for finCxaTransaçãoDestino and a register for FinCxaTransaçãoOrigem;

Remembering that in the structure, the FinCxaPlanoLcto is LazyLoading ... When searching for finCxaTransacaoDestino.getFinCxaPlanoLctos () this list returns with duplicate records, that is, the return is 4 items and in the database contains only two, being a duplication for each record.

However, in the object, this list returns only one record, without duplication.

I've researched several examples where people solve using Set instead of List for the OneToMany relationship, this solves my problem, but in other situations it does not allow me to insert duplicate values when necessary.

I would like to know why JPA is duplicating records and if there is a solution?

    
asked by anonymous 01.07.2016 / 20:02

1 answer

1

The solution I did to my problem was to convert the list to a Set < > and then add it back to the List and return it. Yes and I consider it a gambiarra, as soon as possible I will make the suggestion passed on and use log4j.

Thank you.

Set finChapterLoctos = new HashSet

05.07.2016 / 19:34