hibernate: Unable to locate appropriate constructor on class - HQL

有些话、适合烂在心里 提交于 2019-12-01 15:41:18

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;

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.

Making a public constructor with arguments may make it work.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!