org.hibernate.MappingException: Named query not known


When calling a query with a specific name, the system throws the exception: org.hibernate.MappingException: Named query not known . I've given it several laps but I can not find where the problem is.

@Table( name = "consumo" )
@NamedQueries( {
                name = "Consumo.findConsumoPasoHistorico",
                query = "SELECT c FROM Consumo c WHERE c.fecha < :fechaTope ORDER BY c.fecha," )
} )
public class Consumo implements Serializable {

    @Basic( optional = false )
    @Column( name = "id" )
    @GeneratedValue( strategy = GenerationType.IDENTITY )
    private Long id;

    @Temporal( TemporalType.TIMESTAMP )
    @Column( name = "fecha" )
    private Date fecha;

    @Column( name = "valor" )
    private double valor;

    @Temporal( TemporalType.TIMESTAMP )
    @Column( name = "created" )
    private Date created;

    @Temporal( TemporalType.TIMESTAMP )
    @Column( name = "updated" )
    private Date updated;

    //Getters y setters

The method from which I invoke the function:

public List<Consumo> findConsumoPasoHistorico(Date fechaTope) {
    Query query = getSession().getNamedQuery("Consumo.findConsumoPasoHistorico");
    query.setParameter("fechaTope", fechaTope);
    return query.list();

I have made sure that the configuration file hibernate.cfg.xml contains the reference to map the Consumption class:

<mapping class="es.kestrel.monitorizador.modelo.Consumo"/>
asked by hecnabae 03.12.2015 в 16:56

2 answers


Try changing the indicated part, since you do not need to indicate the class:

public List<Consumo> findConsumoPasoHistorico(Date fechaTope) {
    //Query query = getSession().getNamedQuery("Consumo.findConsumoPasoHistorico");
    Query query = getSession().getNamedQuery("findConsumoPasoHistorico");
    query.setParameter("fechaTope", fechaTope);
    return query.list();
answered by 03.12.2015 / 17:05

I usually call them using the method createNamedQuery of Entity Manager .

Query query = em.createNamedQuery("Consumo.findConsumoPasoHistorico", Consumo.class);
List resultados = query.getResultList();

For more information you can consult link .

answered by 03.12.2015 в 17:11