John Boudreaux
John Boudreaux

Reputation: 105

Reactive Panache relation JsonMappingException: Collection cannot be initialized

The non-reactive version of this code works fine. But in the reactive version, something happens when mapping a null or empty collection from the database.

The POST of a new Template object returns a 201 with nothing unusual in the logs. But, when I do the GET on Template, the listAll() returns the error below.

I've tried initializing the "sections" member to an empty collection, but the result is the same.

What am I missing?

The Reactive Entity:

import io.quarkus.hibernate.reactive.panache.PanacheEntity;

@Entity
public class Template extends PanacheEntity {
  public String name;
  @OneToMany(mappedBy = "template", cascade = CascadeType.ALL)
  public List<Section> sections;
}

The Resource API:

  @GET
  @Path("template")
  public Uni<List<Template>> listTemplates() {
    return Template.<Template>listAll();
  }

  @POST
  @Path("template")
  @Consumes("application/json")
  @Produces("application/json")
  @ReactiveTransactional
  public Uni<Response> addTemplate(Template template) {
    return Panache.<Template>withTransaction(template::persist)
            .onItem().transform(inserted -> {
              return createdResponse("/template/%d", inserted.id);
        });
  }

The Dependencies:

      <artifactId>quarkus-resteasy-reactive-jackson</artifactId>
      <artifactId>quarkus-hibernate-reactive-panache</artifactId>
      <artifactId>quarkus-resteasy-reactive</artifactId>
      <artifactId>quarkus-reactive-pg-client</artifactId>

The error:

JsonMappingException: HR000056: Collection cannot be initialized: score.Template.sections (through reference chain: java.util.ArrayList[0]->score.Template["sections"])

Upvotes: 3

Views: 338

Answers (1)

user20285570
user20285570

Reputation: 1

try this workaround fetch = FetchType.EAGER

import io.quarkus.hibernate.reactive.panache.PanacheEntity;
@Entity
public class Template extends PanacheEntity {
  public String name;
  @OneToMany(mappedBy = "template", cascade = CascadeType.ALL,fetch = FetchType.EAGER)
  public List<Section> sections;
}

Upvotes: 0

Related Questions