Search code examples
javaquarkusquarkus-panache

How to select only certain fields with Quarkus Panache?


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?


Solution

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