简体   繁体   中英

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 ?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM