0

We started Quarkus 3 project with hibernate reactive. We tried panache implementation .project() but it is not working as expected. Is there any solution for this problem? Is possible to manually do projection or something?

Error:

java.lang.IllegalStateException: Could not determine appropriate instantiation strategy - no matching constructor found and one or more arguments did not define alias for bean-injection
at org.hibernate.sql.results.graph.instantiation.internal.DynamicInstantiationResultImpl.resolveAssembler(DynamicInstantiationResultImpl.java:197)
at org.hibernate.sql.results.graph.instantiation.internal.DynamicInstantiationResultImpl.createResultAssembler(DynamicInstantiationResultImpl.java:106)

Entities:

@Entity
@Table(name = "company")
public class CompanyEntity extends PanacheEntity {
    private String name;
    private String address;

    @OneToMany(mappedBy = "company", targetEntity = DepartmentEntity.class, 
               cascade = CascadeType.ALL, fetch = FetchType.EAGER,
               orphanRemoval = true)
    private List<DepartmentEntity> departments = new ArrayList<>();

    // getters and setters omitted for brevity
}
@Entity
@Table(name = "department")
public class DepartmentEntity extends PanacheEntity {
    private String name;
    private String description;

    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    private CompanyEntity company;

    // getters and setters omitted for brevity
}

DTOs

public class CompanyProjection {
    private String name;
    private List<DepartmentProjection> departments;

    public CompanyProjection(String name, List<DepartmentProjection> departments) {
        this.name = name;
        this.departments = departments;
    }

    // getters and setters omitted for brevity
   
}
public class DepartmentProjection {
    private String description;

    public DepartmentProjection(String description) {
        this.description = description;
    }

    // getters and setters omitted for brevity
}

Related repository method:

@ApplicationScoped
public class CompanyRepository implements PanacheRepository<CompanyEntity> {

    // ...

    @WithSession
    public Uni<List<CompanyProjection>> getCompanyProjections() {
        return findAll().project(CompanyProjection.class).list();
    }
}

Code sample is available on GitHub.

zforgo
  • 2,508
  • 2
  • 14
  • 22

1 Answers1

1

The projection class is applied to each row of the result of the query. The problem is that the constructor expect a list. A list usually implies multiple rows. That's why you see the exception.

I don't think there's a way for Hibernate Reactive or Panache to map automatically the result of the association the way you need.

Davide D'Alto
  • 7,421
  • 2
  • 16
  • 30