JPQL Order by and LIMIT (JSF and JPA)

0

I'm having trouble solving a query with JPQL.

I want it to work:

SELECT * from tab_historico
WHERE idPolicial = 1
ORDER BY idhistorico DESC
LIMIT 1

My DAO is wrong, where I need to put the query:

public Historico obterMarket()
{
    String jpql = "from Historico h";

    jpql = jpql + " order by h.idhistorico DESC";


    TypedQuery<Historico> comando = this.getEntityManager().createQuery(jpql, Historico.class);

    return  comando.getSingleResult();
}

My managed bean is working:

package modelo.dominio;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;

import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Marker;

import modelo.dao.HistoricoDAO;

@ManagedBean(name= “markersView”)
public class MarkersView implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private MapModel simpleModel;

    @PostConstruct
    public void init() {
        simpleModel = new DefaultMapModel();
        HistoricoDAO dao = new HistoricoDAO();


        String[] separated = dao.obterMarket().getLatLon().split(",");
        String lat = separated[0];
        String lon = separated[1];

        LatLng coord1 = new LatLng(Double.valueOf(lat),Double.valueOf(lon));

        simpleModel.addOverlay(new Marker(coord1, "Pacote Armamento FAKE"));

    }

    public MapModel getSimpleModel() {
        return simpleModel;
    }

}
    
asked by anonymous 23.09.2018 / 20:15

1 answer

0

Try to do the search this way:

public Historico obterMarket() {
   return this.entityManager.createQuery("SELECT h FROM Historico h ORDER BY h.idhistorico DESC", Historico.class).getSingleResult();
}
    
24.09.2018 / 01:05