hibernate: Unable to locate appropriate constructor on class - HQL

could not locate appropriate constructor on class spring boot
org.hibernate.hibernateexception: unable to call class constructor
cause=org hibernate propertynotfoundexception no appropriate constructor in class
org hibernate hql internal ast querysyntaxexception unable to locate class
spring could not locate appropriate constructor on class

When I trying to execute this HQL to return an object Ponto I receive this error:

ERROR [org.hibernate.hql.PARSER] (http-localhost-127.0.0.1-8080-2) Unable to locate appropriate constructor on class [br.com.cdv.model.entity.Ponto] [cause=org.hibernate.PropertyNotFoundException: no appropriate constructor in class: br.com.cdv.model.entity.Ponto]

DAO

    @SuppressWarnings("unchecked")
    @Override
    public List<Ponto> listLoja(Integer idLoja) {

        Query q = getSession().createQuery("select new Ponto(0,ss.cliente,ss.loja,null,null,null,null,null,sum(qtdPontos),'',0) "
            + "from Ponto as ss where ss.loja.id = :idLoja "
            + "group by ss.cliente, ss.loja");  

        q.setParameter("idLoja", idLoja);

        return (List<Ponto>) q.list();
    }  

My Entity / Class

@Entity
@Table (name = "ponto")
public class Ponto implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private Integer id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="cliente", nullable=true)
    private UsuarioCliente cliente;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="loja", nullable=false)
    private UsuarioLoja loja;

    @Column(name="dataCriacao")
    private Date dataCriacao;

    @Column(name="dataUtilizado", length=12, nullable=true)
    private Date dataUtilizado;

    @Column(name="dataExpira")
    private Date dataExpira;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "funcionario", nullable=true)
    private Funcionario funcionario;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "pontoReceber", nullable=true)
    private PontoReceber pontoReceber;

    @Column(name="qtdPontos", nullable=false)
    private long qtdPontos;

    @Column(name="obsPontos", nullable = true,length=300)
    private String obsPontos;

    @NotEmpty
    @Column(name="tipo",nullable = false)
    private Integer tipo;

    public Ponto(Integer id, UsuarioCliente cliente,UsuarioLoja loja, Date dataCriacao, Date dataUtilizado,
              Date dataExpira, Funcionario funcionario, PontoReceber pontoReceber, long qtdPontos, String obsPontos, Integer tipo) {
        setId(id);
        setCliente(cliente);
        setLoja(loja);
        setDataCriacao(dataCriacao);
        setDataUtilizado(dataUtilizado);
        setDataExpira(dataExpira);
        setFuncionario(funcionario);
        setPontoReceber(pontoReceber);
        setQtdPontos(qtdPontos);
        setObsPontos(obsPontos);
        setTipo(tipo);
    }
        // getters and setters
    }

Control:

@RequestMapping("/listarClientes")
    public String listarClientesPontos(Map<String, Object> map, HttpSession session) {

        ...

        List<Ponto> pontos = pontoService.listLoja(loja.getId());

        map.put("pontos", pontos);

        return "listaClientesPonto";
    }
}

View:

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>


    <body>
        <h2>Lista Clientes</h2>

        <table>
          <tr>
            <th>ID Cliente</th>
            <th>CPF Cliente</th>
            <th>Pontos totais:</th>
          </tr>

          <c:forEach items="${pontos}" var="ponto" varStatus="count">
          <tr>
            <td>${ponto.cliente.id}</td>
            <td>${ponto.cliente.cpf}</td>
            <td>${ponto.qtdPontos}</td>
          </tr>
          </c:forEach>
        </table>
    </body>

Why I am getting this error? Has any better why to receive this Object Ponto in a List?

obs.. without new Ponto(...) returns a list of Ponto with unidentified Objects[]

Check these things:

1- If you make a constructor with parameters; you should provide the constructor with no parameters, explicity;

2- Make sure your ID entity is int/Integer;

3- Make your Entity java.io.Serializable by implementing;

4- Make your parameter-less (default) constructor public or default access modifier;

QuerySyntaxException "Unable to locate appropriate constructor", literal in SELECT NEW JPQL query. Description. Enum: DTO class: Expects the following query to work: but Hibernate throws error: As mentioned here� Trying to run the following JPA criteria query on Hibernate 5.0.6 final returning a single value of type java.time.LocalDateTime in Java 8. CriteriaBuilder criteriaBuilder = entityManager.

Found the problem... I made some bad constructors, so I edited the constructors in my entity:

@Entity
@Table (name = "ponto")
public class Ponto implements java.io.Serializable {

    @Id
    @GeneratedValue
    private Integer id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="cliente", nullable=true)
    private UsuarioCliente cliente;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="loja", nullable=false)
    private UsuarioLoja loja;

    @Column(name="dataCriacao")
    private Date dataCriacao;

    @Column(name="dataUtilizado", length=12, nullable=true)
    private Date dataUtilizado;

    @Column(name="dataExpira")
    private Date dataExpira;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "funcionario", nullable=true)
    private Funcionario funcionario;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "pontoReceber", nullable=true)
    private PontoReceber pontoReceber;

    @Column(name="qtdPontos", nullable=false)
    private long qtdPontos;

    @Column(name="obsPontos", nullable = true,length=300)
    private String obsPontos;

    @NotEmpty
    @Column(name="tipo",nullable = true)
    private Integer tipo;

    public Ponto() {
    }

    public Ponto(UsuarioCliente cliente, UsuarioLoja loja, long qtdPontos) {
        this.cliente = cliente;
        this.loja = loja;
        this.qtdPontos = qtdPontos;
    }
    // getters and setters
}

and HQL:

    Query q = getSession().createQuery("select new Ponto(ss.cliente,ss.loja,sum(ss.qtdPontos) as qtdPontos) "
            + "from Ponto as ss where ss.loja.id = :idLoja "
            + "group by ss.cliente, ss.loja");
    q.setParameter("idLoja", idLoja);

I am crying like a baby, four days with this issue.

Thanks for the directions Thufir Hawat.

Unable to find constructor when using new class , hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate appropriate constructor on class: Root cause: If you are trying to populate� Hibernate ORM. Classic software project HHH-9875. Copy the link to this issue. summary. QuerySyntaxException "Unable to locate appropriate constructor" using enum

Making a public constructor with arguments may make it work.

Unable to locate appropriate constructor, Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast. QuerySyntaxException: Unable to locate appropriate constructor on class� When I trying to execute this HQL to return an object Ponto I receive this error: ERROR [org.hibernate.hql.PARSER] (http-localhost-127.0.0.1-8080-2) Unable to locate appropriate constructor on

If you have made a selection (i.e. don't return all the columns of the table), make sure to have a constructor with the selected column(s) in your mapped class.

Unable to locate appropriate constructor (Object Relational Mapping , [color=red]org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [com.lj.core.model.Channel] [select new� When a constructor JPQL query projects a Timestamp, it causes an org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor. For example, given the following Entity: @Entity public class Employee { private Timestamp lastUpdatedAsTimestamp;} and the following Pojo for the constructor query:

SOLVED: Caused by: org.hibernate.hql.internal.ast , org.hibernate.hql.internal.ast.DetailedSemanticException: Unable to locate appropriate constructor on class. Check this. 1. 기본생성자 체크. MyEntity attrubute s: int id , Double income , Integer times ; my query: hql = "select new myObject(sum(me.income),sum(me.times) ) from MyEntity me " ; Query q = session.createQue

Unable to locate appropriate constructor on class , getConstructor() if it cannot // locate an appropriate constructor throw new DetailedSemanticException( "Unable to locate appropriate constructor on class [" +� I believe you need to follow the JavaBean convention in your business objects if you want to use hibernate with them. You need a no-arg constructor as well as public getters and setters for all the fields you want persisted.

Unable to locate appropriate constructor on class, org.hibernate.util.ReflectHelper is the class used to find the appropriate constructor. But the code used to do it calls clazz.getConstructors(), getting only the public contructors:

Comments
  • glad to know. You are welcome, greetings from Brazil! I'm here too ;)