JPQL Order by and LIMIT (JSF and JPA)

Asked

Viewed 51 times

0

I’m having trouble solving a query with JPQL.

Query I need to work:

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

My DAO that is wrong, where I need to place 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 bean Managed that 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;
    }

}

1 answer

0

Try doing the research this way:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.