question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Simplify projection of a single field when using Panache with Hibernate

See original GitHub issue

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()

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:19 (17 by maintainers)

github_iconTop GitHub Comments

1reaction
FroMagecommented, Oct 28, 2022

No, the signature of .find is

<T extends PanacheEntityBase> PanacheQuery<T> find(String query,  ... )

So it needs to be an entity class

That signature was a mistake, I’ve wanted to drop that constraint for a long time. We should really drop it.

1reaction
DavideDcommented, Oct 27, 2022

To be fair Hibernate already handle all these scenarios.

If one select a single field, it will return a list of the type of the field. Otherwise, it will return a List<Object[]>.

I think we should do the same:

List<String> names = Person.findAll().project("name").list();
List<Object[]> custom = Person.findAll().project("name", "status").list();

People can then handle the list the way they prefer.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Simplified Hibernate ORM with Panache - Quarkus
Query projection can be done with the project(Class) method on the PanacheQuery object that is returned by the find() methods. You can use...
Read more >
How to select only certain fields with Quarkus Panache?
All the guides explain how to write different queries, but is not clear how to select only certain attributes.
Read more >
Simplified Hibernate ORM with Panache - 《Quarkus ... - 书栈网
Paging; Using a range instead of pages; Sorting; Simplified queries; Named queries; Query parameters; Query projection.
Read more >
Simplified MongoDB with Panache - Quarkus
Query projection can be done with the project(Class) method on the PanacheQuery object that is returned by the find() methods. You can use...
Read more >
Panache Query Projection Relations · Issue #10437 - GitHub
quarkusbot added the area/panache label on Jul 2, 2020 ... So I should update my reproducer using only hibernate and if it's a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found