Skip to content

Simplify projection of a single field when using Panache with Hibernate #28844

Open
@DavideD

Description

With the current version of Panache with Hibernate, if we want to project a field from an entity, we need to do this:

import io.quarkus.runtime.annotations.RegisterForReflection;

@RegisterForReflection 
public class PersonName {
    public final String name; 

    public PersonName(String name){ 
        this.name = name;
    }
}

// only 'name' will be loaded from the database
PanacheQuery<PersonName> query = Person.find("status", Status.Alive).project(PersonName.class);

It seems a lot of work for returning a single field.

It would be nice ot have a way to specify a field:

PanacheQuery<String> query = Person.find("status", Status.Alive).project("name");

or, maybe,

PanacheQuery<String> query = Person.find("status", Status.Alive).project(Person.attribute("name", String.class));

For comparison, using HIbernate one can do:

session.createQuery("select name from Fruit ...")

This issue applies to both Panache with Hibernate ORM and Panache with Hibernate Reactive

Maybe we should also consider what happens when a user run a query like this:

List<String> distinctNames = Person.find("select distinct p.name from Person p").???.list();

One idea:

List<String> names = Person.distinct().find("status", Status.Alive).project("name", String.class).list()

WORKAROUND:
I haven't tested but it seems that this will work:

List<String> names = Person
    .find("select distinct name from Person where ...")
    .project(String.class)
    .list()

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions