如何在Quarkus Panache中仅选择特定字段?

huangapple go评论69阅读模式
英文:

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> {

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

   // ! 而且这就是我尝试过的,但是这种方式是不可能的
   // 所有的方法都返回Person或类似Person的类型,比如List<Person>
   // 所以基本上这根本无法编译通过
   public List<String> findAllLastNames() {
       return this.find("select p.lastName from Person p").list();
   }
}

所有的指南都解释了如何编写不同的查询,但没有清楚地说明如何只选择特定的属性。

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

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&lt;Person&gt; {

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


   // ! and this is what I tried, but it&#39;s not possible to do it this way
   // all the methods return Person or something of type Person like List&lt;Person&gt;
   // so basically this won&#39;t even compile
   public List&lt;String&gt; findAllLastNames() {
       return this.find(&quot;select p.lastName from Person p&quot;).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?

答案1

得分: 8

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

请毫不犹豫地投票支持它(+1 反应)并提供反馈。

英文:

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.

答案2

得分: 8

@aksappy 所说,此功能已添加,其文档可在此处找到。

英文:

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

答案3

得分: 0

可以使用查询参数 ->

    Map&lt;String, Object&gt; params = new HashMap&lt;&gt;();
    params.put(&quot;fisrtName&quot;, fisrtName);
    params.put(&quot;lastName&quot;, lastName);

    final List&lt;String&gt; enetityList = Person.list(&quot;fisrtName= :fisrtName and lastName= :lastName&quot;, params);

参考链接 - https://quarkus.io/guides/hibernate-orm-panache

英文:

You can use Query Parameters ->

    Map&lt;String, Object&gt; params = new HashMap&lt;&gt;();
    params.put(&quot;fisrtName&quot;, fisrtName);
    params.put(&quot;lastName&quot;, lastName);

    final List&lt;String&gt; enetityList = Person.list(&quot;fisrtName= :fisrtName and lastName= :lastName&quot;, params);

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

huangapple
  • 本文由 发表于 2020年4月10日 16:59:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/61137039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定