org.hibernate.MappingException: Named query not known

5

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.

Consumo.java

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

    @Id
    @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
source

2 answers

0

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
source
0

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