Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite recursion due to @JsonIgnoreProperties not applicable to List valued property #70

Closed
peruzzo opened this issue May 26, 2015 · 6 comments
Milestone

Comments

@peruzzo
Copy link
Contributor

peruzzo commented May 26, 2015

I have a stackoverflow problem, but it occurs only depending on the entity that initialize first.

@Entity @Table(name="contrato")
public class Contrato {

    @Id
    private Long id;

    private String numeroContrato;

    @OneToMany(mappedBy="contrato") @JsonIgnoreProperties("contrato")
    private List<Parcela> parcelas;

    @OneToMany(mappedBy="contrato") @JsonIgnoreProperties("contrato")
    private List<Liquidacao> liquidacoes;
}

@Entity @Table(name="contrato_parcela")
public class Parcela {

    @Id
    private Long id;

    private Integer numeroParcela;  

    @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="contrato_id")
    private Contrato contrato;
}

@Entity @Table(name="contrato_liquidacao")
public class Liquidacao {

    @Id
    private Long id;

    private BigDecimal valorTotal;

    @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="contrato_id")
    private Contrato contrato;

    @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="contrato_parcela_id") @JsonIgnoreProperties("contrato")
    private Parcela parcela;
}

This block below works perfectly with the expected return: {"numeroContrato":"101-6861944","parcelas":[{"numeroParcela":3}],"liquidacoes":[{"valorTotal":91548.00,"parcela":{"numeroParcela":3}}]}

final Contrato contrato = em.find(Contrato.class, 1L);
Hibernate.initialize(contrato.getParcelas());
Hibernate.initialize(contrato.getLiquidacoes());
mapper.writer().writeValueAsString(contrato);

But that other code block, where I changed the boot order of the relationship I have a stackoverflow.

Contrato contrato = em.find(Contrato.class, 1L);
Hibernate.initialize(contrato.getLiquidacoes());
Hibernate.initialize(contrato.getParcelas());
mapper.writer().writeValueAsString(contrato); 

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: teste.jackson.Contrato["parcelas"]->org.hibernate.collection.internal.PersistentBag[0]->teste.jackson.Parcela["contrato"]->teste.jackson.Contrato["parcelas"]->org.hibernate.collection.internal.PersistentBag[0]->teste.jackson.Parcela["contrato"]->teste.jackson.Contrato["parcelas"]->org.hibernate.collection.internal.PersistentBag[0]->teste.jackson.Parcela["contrato"]->teste.jackson.Contrato["parcelas"]->org.hibernate.collection.internal.PersistentBag[0] ...

@peruzzo
Copy link
Contributor Author

peruzzo commented May 27, 2015

I believe that solving the problem is to make the class HibernateProxySerializer implement the ContextualSerializer interface. This way you can have accesso to BeanProperty in serialize method.

@mperuzzo
Copy link

+1

@cowtowncoder
Copy link
Member

What would be useful here is a self-contained unit test.

peruzzo added a commit to peruzzo/jackson-datatype-hibernate that referenced this issue Dec 27, 2015
cowtowncoder added a commit that referenced this issue Dec 28, 2015
@cowtowncoder cowtowncoder changed the title Infinite recursion Infinite recursion due to @JsonIgnoreProperties not applicable to List valued property Dec 29, 2015
peruzzo added a commit to peruzzo/jackson-datatype-hibernate that referenced this issue Dec 29, 2015
@peruzzo
Copy link
Contributor Author

peruzzo commented Jan 11, 2016

@cowtowncoder He came to look at my suggestion to fix this issue #82 ? I try to see the other way if you do not find a valid solution.

@cowtowncoder
Copy link
Member

@peruzzo Thanks -- I'll have a look at this now.

@peruzzo
Copy link
Contributor Author

peruzzo commented Jan 15, 2016

@cowtowncoder Thanks! 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants