When invoking a query with specified name the system throws the exception: org.hibernate.MappingException: Named query not known
. I've given it several turns but I can't find where the problem is.
Consumption.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 call 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"/>
Try changing the marked part, since you don't need to indicate the class:
I normally call them using the
createNamedQuery
del methodEntity Manager
.For more information you can consult http://www.objectdb.com/java/jpa/query/named .