JPQL - 如何在我的实体中获得额外的参数?

JPQL - How can I get an extra param inside my entity?

我是 JPQL 的新手,我正在尝试获取实体中不存在的额外参数,但我不知道如何去做。

通过在论坛中搜索,我发现有些使用 DTO,但我不知道如何应用它。

这是我的实体:

@Entity
@Table(name = "person")
public class Person implements Serializable {

    private int id;
    private String name;
    private String email;
    private int age;

...
}

还有我的 JPQL:

SELECT COUNT(a.name) as countOfNames, a FROM Person a WHERE a.name like :name

我怎样才能在 myentity 对象中获取 countOfNames 结果,因为它不是列?

最简单的方法是使用构造函数表达式

package com.entites

public class PersonDto {

    private Person person;

    private Integer countOfNames;

    public PersonDto(Person person, Integer countOfNames) {
        this.person = person;
        this.countOfNames = countOfNames;
    }

}

select new com.entites.PesronDto(a, count(a.name)) 
  from Person a 
  where a.name like :name