如何使用 Quarkus Panache select 仅某些字段?

How to select only certain fields with Quarkus Panache?

Quarkus 使用 Panache.

简化了 Hibernate ORM 映射

这是我的 entityPanacheRepository 的示例:

@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();
   }

}

所有指南都解释了如何编写不同的查询,但不清楚如何 select 仅某些属性。

如果我不需要整个 Person 对象,而是我数据库中所有人的 lastName

是否可以 select 仅 Quarkus Panache 某些属性?

这目前是不可能的,您可以订阅这个关于使用 Panache 进行 Hibernate 投影的问题:https://github.com/quarkusio/quarkus/issues/6261

毫不犹豫地为其投票(+1 反应)并提供反馈。

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