Retrieve object with parameter type date JPA, JPQL, JSF, EJB

0

Good day, I have the following query I want to search for an object by means of the DATE_ORDEN_TRABAJO the object ORDENTRABAJO I retrieve it very easily by its ID by means of the following query.

Entity Consultation

@NamedQuery(name = "Ordentrabajo.findById", query = "SELECT o FROM Ordentrabajo o WHERE o.id = :id"),

Bean

@ManagedBean
@ViewScoped
public class EstadoBean implements Serializable {

@EJB
private OrdentrabajoFacade ordentrabajoFacade; 

private Integer idOrdentrabajoABuscar;


public Integer getIdOrdentrabajoABuscar() {
    return idOrdentrabajoABuscar;
}

public void setIdOrdentrabajoABuscar(Integer idOrdentrabajoABuscar) {
    this.idOrdentrabajoABuscar = idOrdentrabajoABuscar;
}

Start

 @PostConstruct
public void inicializarBean() {

    tipoEstadoList = tipoEstadoFacade.findAll();
    ordentrabajoEstadoList = ordentrabajoEstadoFacade.findAll();

    ordenTrabajoList = new ArrayList<>();
    ordentrabajoListFecha = new ArrayList<>();
    ordentrabajoEstado = new OrdentrabajoEstado();
    fechaActual = Calendar.getInstance().getTime();

}

Logic method

   public void listenerBotonBuscarOrdentrabajo() {
    if (idOrdentrabajoABuscar != null) {
        String namedQuery = "Ordentrabajo.findById";
        Map<String, Object> parametros = new HashMap<>();
        parametros.put("id", idOrdentrabajoABuscar);
        ordenTrabajoList =    ordentrabajoFacade.findByNamedQuery(namedQuery, parametros);

    } else {
        lanzarMensajeError("Debe Ingresar un ID de Orden de Trabajo");
    }

}

Screen method

       <p:panelGrid columns="1">

                <p:outputLabel for="itidOrdentrabajo" value="INGRESE ID ORDENTRABAJO"/>                    
                <p:inputText id="itidOrdentrabajo" value="#{estadoBean.idOrdentrabajoABuscar}"  required="true" requiredMessage="Debe ingresar un valor"/>
                <p:message for="itidOrdentrabajo"/>                    
                <p:separator/>                    


                <p:commandButton value="BUSCAR" actionListener="#{estadoBean.listenerBotonBuscarOrdentrabajo()}"
                                 process="@this, itidOrdentrabajo"
                                 update="@form">                    
                </p:commandButton>

            </p:panelGrid> 

This works for me in a different way if I wanted to retrieve the method by means of the DATE_ORDENTRABAJO since it does not generate anything or an error or absolutely nothing.

Entity Consultation

 @NamedQuery(name = "Ordentrabajo.findByFechaOrdenTrabajo", query = "SELECT o FROM Ordentrabajo o WHERE o.fechaOrdenTrabajo = :fechaOrdenTrabajo"),

Bean

@ManagedBean
@ViewScoped
public class EstadoBean implements Serializable {
private Date capturarFecha;
  public Date getCapturarFecha() {
    return capturarFecha;
}

public void setCapturarFecha(Date capturarFecha) {
    this.capturarFecha = capturarFecha;
}

}

Start

 @PostConstruct
public void inicializarBean() {

    tipoEstadoList = tipoEstadoFacade.findAll();
    ordentrabajoEstadoList = ordentrabajoEstadoFacade.findAll();

    ordenTrabajoList = new ArrayList<>();
    ordentrabajoListFecha = new ArrayList<>();
    ordentrabajoEstado = new OrdentrabajoEstado();
    fechaActual = Calendar.getInstance().getTime();

}

Logica Method

public void listenerBotonBuscarOrdentrabajoPorFecha() {        
    if (capturarFecha != null) {
        String namedQuery = "Ordentrabajo.findByFechaOrdenTrabajo";
        Map<String, Object> parametros = new HashMap<>();
        parametros.put("fechaOrdenTrabajo", capturarFecha);
        ordentrabajoListFecha = ordentrabajoFacade.findByNamedQuery(namedQuery, parametros);
    }
}

Screen

<p:panelGrid columns="1">

                <p:outputLabel for="itFechaDesde" value="Fecha"/>                    
                <p:calendar id="itFechaDesde" value="#{estadoBean.capturarFecha}"  required="true" requiredMessage="Debe ingresar un valor"
                            pattern="MM/dd/yyyy HH:mm:ss"/>
                <p:message for="itFechaDesde"/>                    
                <p:separator/>                    

                <p:commandButton value="BUSCAR" actionListener="#{estadoBean.listenerBotonBuscarOrdentrabajoPorFecha()}"
                                 process="@this, itFechaDesde"
                                 update="@form">                    
                </p:commandButton>

            </p:panelGrid>
    
asked by Alexander Gil Tafur 31.07.2017 в 17:19
source

1 answer

0

For the dates it is good to choose to take them in a range, since the date comparison is made even at the level of thousands, so the = is not so effective, you can use

SELECT o FROM Ordentrabajo o WHERE o.fechaOrdenTrabajo BETWEEN :fechaInicial AND :fechaFinal"

and the detail to where you want to restrict the range, depends on the need that you have, that it is on the same day, or at the same time or at the same minute, in any case it places the initial date as the one that you need the final date you add one according to the range you need to delimit
ejm Startdate = 2017-01-01 12:00:00 End date = 2017-01-01 12:01:00

Another point that can affect is the format in which you send it, ensure that the conversion to string is done as specified in the database format

    
answered by 31.07.2017 / 17:28
source