Simplify projection of a single field when using Panache with Hibernate
See original GitHub issueWith 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:
- Created a year ago
- Comments:19 (17 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
That signature was a mistake, I’ve wanted to drop that constraint for a long time. We should really drop it.
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:
People can then handle the list the way they prefer.