1

I have a JPA code with OneToMany relationship. A Customer has a list of Item to check out. However, the code continue to generate StackOverflowError.

Once, I had resolved this one by applying @JsonIgnore while fetching the List<Item> from Customer entity. But even that does not seem to work anymore.

In Customer class:

@OneToMany(mappedBy = "customer", orphanRemoval = true)
@JsonIgnore
private List<Item> items;

In Item class:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CUSTOMER_ID", nullable = false)
private Customer customer;

And CustomerRest class:

@Path("customers")
public class CustomerRest {

    @Inject
    NewSessionBean newSessionBean;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Customer> getAllCustomers() {
        return newSessionBean.getCustomers();
    }
}

Method newSessionBean.getCustomers():

public List<Customer> getCustomers(){
    TypedQuery<Customer> q= em.createQuery("select c from Customer c", Customer.class);

    return q.getResultList();
}

I expect a nicely formatted JSON message but there is no sign of this. All I get is the java.lang.StackOverflowError on the browser and the Server log generates the following:

Generating incomplete JSON|#]
    java.lang.StackOverflowError
    java.lang.StackOverflowError    at org.eclipse.yasson.internal.serializer.DefaultSerializers.findByCondition(DefaultSerializers.java:130)
Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
Singam
  • 443
  • 3
  • 12
  • You need to take a look at: `@JsonManagedReference` and `@JsonBackReference` annotations. Read: [Jackson – Bidirectional Relationships](https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion). See also: [Searching proper way to convert my two-way linkage between objects to JSON format](https://stackoverflow.com/questions/54853178/searching-proper-way-to-convert-my-two-way-linkage-between-objects-to-json-forma), [Jackson/Hibernate, meta get methods and serialization](https://stackoverflow.com/questions/55383889/jackson-hibernate-meta-get-methods-and-serialization) – Michał Ziober Aug 03 '19 at 08:02
  • Thanks. But, I did try JsonManagedReference and JsonBackReference - in vain. – Singam Aug 04 '19 at 13:58
  • 1
    You're not using Jackson to serialize your objects to JSON. So using Jackson annotations won't fix anything. Read your stacktrace: `at org.eclipse.yasson.internal.serializer.DefaultSerializers.findByCondition(DefaultSerializers.java:130)`. You're using yasson, not Jackson. – JB Nizet Aug 04 '19 at 16:14

1 Answers1

3

It looks like you use Yasson project not Jackson. In that case you should use @JsonbTransient annotation. See documentation:

By default, JSONB ignores properties with a non public access. All public properties - either public fields or non public fields with public getters are serialized into JSON text.

Excluding properties can be done with a @JsonbTransient annotation. Class properties annotated with @JsonbTransient annotation are ignored by JSON Binding engine. The behavior is different depending on where @JsonbTransient annotation is placed.

See also:

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146