8

Quarkus simplifies Hibernate ORM mappings with Panache.

Here is an example of my entity and PanacheRepository:

@Entity
public class Person {
    @Id @GeneratedValue private Long id;
    private String firstName;
    private String lastName;
    private LocalDate birth;
    private Status status;
}

@ApplicationScoped
public class PersonRepository implements PanacheRepository<Person> {

   // example
   public Person findByName(String name){
       return find("name", name).firstResult();
   }


   // ! and this is what I tried, but it's not possible to do it this way
   // all the methods return Person or something of type Person like List<Person>
   // so basically this won't even compile
   public List<String> findAllLastNames() {
       return this.find("select p.lastName from Person p").list();
   }

}

All the guides explain how to write different queries, but is not clear how to select only certain attributes.

If I don't need the whole Person object, but rather the lastName of all persons in my DB?

Is it possible to select only certain attributes with Quarkus Panache?

ikos23
  • 4,879
  • 10
  • 41
  • 60
  • This is a good question. I have the same issue including returning aggregates like SUM() in select – Aiden Dec 06 '20 at 11:05
  • @Aiden you can vote it up if you think it is useful:) – ikos23 Dec 06 '20 at 17:10
  • Quarkus has introduced projection which will use a custom pojo to dynamically identify the required columns to use in query. See documentation [here](https://quarkus.io/guides/hibernate-orm-panache#query-projection) – aksappy Nov 09 '21 at 21:15
  • 1
    @aksappy Thank you :) I think this comment is helpful, but if you have time I think it might be valuable to add it as an answer with a little details - because it looks like current approved answer is not valid anymore. Also you can vote up this question if you think it might be useful for other people. – ikos23 Nov 10 '21 at 09:31

3 Answers3

8

This is currently not possible, you can subscribe to this issue regarding projection for Hibernate with Panache: https://github.com/quarkusio/quarkus/issues/6261

Don't hesistate to vote for it (+1 reaction) and provides feedback.

loicmathieu
  • 5,181
  • 26
  • 31
8

As @aksappy said, this feature was added and it's documentation is available here.

daegyu
  • 96
  • 1
  • 2
0

You can use Query Parameters ->

    Map<String, Object> params = new HashMap<>();
    params.put("fisrtName", fisrtName);
    params.put("lastName", lastName);

    final List<String> enetityList = Person.list("fisrtName= :fisrtName and lastName= :lastName", params);

reference - https://quarkus.io/guides/hibernate-orm-panache

Derrick
  • 3,669
  • 5
  • 35
  • 50