Banns Banns - 2 years ago 81
Java Question

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

I am new to JPQL and I am trying to get an extra parameter that is not in the entity, but I am not finding how to do it.

Through searched here in the forum I found out that some uses a DTO for it, but I am not knowing how to apply that.

Here is my entity:

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

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


And my JPQL:

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

How can I get the
result inside of myentity object since it is not a column?

Answer Source

The simplest way is to use a constructor expression

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( 
  from Person a 
  where like :name
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download