简体   繁体   English

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

[英]How to select only certain fields with Quarkus Panache?

Quarkus simplifies Hibernate ORM mappings with Panache . Quarkus使用Panache简化了 Hibernate ORM 的映射。

Here is an example of my entity and PanacheRepository :这是我的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();
   }

}

All the guides explain how to write different queries, but is not clear how to select only certain attributes.所有指南都解释了如何编写不同的查询,但不清楚如何 select 只有某些属性。

If I don't need the whole Person object, but rather the lastName of all persons in my DB?如果我不需要整个Person lastName ,而是我数据库中所有人的姓氏?

Is it possible to select only certain attributes with Quarkus Panache ?是否有可能 select 仅使用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目前这是不可能的,您可以订阅这个关于 Hibernate 投影的问题: https://github.com/quarkusio/quarkus/issues/6261

Don't hesistate to vote for it (+1 reaction) and provides feedback.不要犹豫投票(+1 反应)并提供反馈。

As @aksappy said, this feature was added and it's documentation is available here .正如@aksappy所说,添加了此功能,其文档可在此处获得。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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