英文:
How to select only certain fields with Quarkus Panache?
问题
Quarkus
使用Panache
简化了Hibernate ORM映射。
以下是我的entity
和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> {
// 示例
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<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
?
答案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 所说,此功能已添加,其文档可在此处找到。
答案3
得分: 0
可以使用查询参数 ->
Map<String, Object> params = new HashMap<>();
params.put("fisrtName", fisrtName);
params.put("lastName", lastName);
final List<String> enetityList = Person.list("fisrtName= :fisrtName and lastName= :lastName", params);
参考链接 - https://quarkus.io/guides/hibernate-orm-panache
英文:
You can use Query Parameters ->
Map<String, Object> params = new HashMap<>();
params.put("fisrtName", fisrtName);
params.put("lastName", lastName);
final List<String> enetityList = Person.list("fisrtName= :fisrtName and lastName= :lastName", params);
reference - https://quarkus.io/guides/hibernate-orm-panache
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论