Java 休眠:无法在类上找到合适的构造函数 - HQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29224093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:36:30  来源:igfitidea点击:

hibernate: Unable to locate appropriate constructor on class - HQL

javaspringhibernatehql

提问by Lipo

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

当我尝试执行此 HQL 以返回一个对象时,Ponto我收到此错误:

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]

错误 [org.hibernate.hql.PARSER] (http-localhost-127.0.0.1-8080-2) 无法在类 [br.com.cdv.model.entity.Ponto] 上找到适当的构造函数 [cause=org.hibernate。 PropertyNotFoundException:类中没有合适的构造函数: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?

为什么我收到这个错误?
为什么在列表中接收这个 Object Ponto 有更好的理由吗?

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

obs.. 不new Ponto(...)返回带有未识别对象的 Ponto 列表[]

采纳答案by Lipo

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:

和 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.

感谢 Thufir Hawat 的指导。

回答by Thufir Hawat

Check these things:

检查这些事情:

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

1- 如果您使用参数创建构造函数;您应该明确地提供不带参数的构造函数;

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

2- 确保您的 ID 实体是 int/Integer;

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

3- 通过实现使您的实体 java.io.Serializable;

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

4- 将您的无参数(默认)构造函数设为 public 或默认访问修饰符;

回答by Smart Coder

Making a public constructor with arguments may make it work.

使用参数创建公共构造函数可能会使其工作。

回答by gdrt

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.

如果您进行了选择(即不返回表的所有列),请确保在您的映射类中有一个带有所选列的构造函数。