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

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

How to select only certain fields with Quarkus Panache?

问题

Quarkus使用Panache简化了Hibernate ORM映射。

以下是我的entityPanacheRepository的示例:

  1. @Entity
  2. public class Person {
  3. @Id @GeneratedValue private Long id;
  4. private String firstName;
  5. private String lastName;
  6. private LocalDate birth;
  7. private Status status;
  8. }
  9. @ApplicationScoped
  10. public class PersonRepository implements PanacheRepository<Person> {
  11. // 示例
  12. public Person findByName(String name){
  13. return find("name", name).firstResult();
  14. }
  15. // ! 而且这就是我尝试过的,但是这种方式是不可能的
  16. // 所有的方法都返回Person或类似Person的类型,比如List<Person>
  17. // 所以基本上这根本无法编译通过
  18. public List<String> findAllLastNames() {
  19. return this.find("select p.lastName from Person p").list();
  20. }
  21. }

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

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

Quarkus Panache中是否可以只选择特定的属性?

英文:

Quarkus simplifies Hibernate ORM mappings with Panache.

Here is an example of my entity and PanacheRepository:

  1. @Entity
  2. public class Person {
  3. @Id @GeneratedValue private Long id;
  4. private String firstName;
  5. private String lastName;
  6. private LocalDate birth;
  7. private Status status;
  8. }
  9. @ApplicationScoped
  10. public class PersonRepository implements PanacheRepository&lt;Person&gt; {
  11. // example
  12. public Person findByName(String name){
  13. return find(&quot;name&quot;, name).firstResult();
  14. }
  15. // ! and this is what I tried, but it&#39;s not possible to do it this way
  16. // all the methods return Person or something of type Person like List&lt;Person&gt;
  17. // so basically this won&#39;t even compile
  18. public List&lt;String&gt; findAllLastNames() {
  19. return this.find(&quot;select p.lastName from Person p&quot;).list();
  20. }
  21. }

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

可以使用查询参数 ->

  1. Map&lt;String, Object&gt; params = new HashMap&lt;&gt;();
  2. params.put(&quot;fisrtName&quot;, fisrtName);
  3. params.put(&quot;lastName&quot;, lastName);
  4. 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 ->

  1. Map&lt;String, Object&gt; params = new HashMap&lt;&gt;();
  2. params.put(&quot;fisrtName&quot;, fisrtName);
  3. params.put(&quot;lastName&quot;, lastName);
  4. 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:

确定