-1

我正在尝试将日期参数传递给我的应用程序,但我遇到了一些困难我的 DAO 看起来像这样:

@Override
public List <Object[]>getMontantParVehicule(Date d1,Date d2) {

    Session session=HibernateUtil.getSessionFactory().openSession();
    try{
    session.beginTransaction();
    System.out.println("Stat HQL");
    SQLQuery q=session.createSQLQuery("select IMMAT,SUM(PRIX) as  COUT,SUM(QUANTITE) as QUANTITE,COUNT(IMMAT) as nbre from BON_ESSENCE where DATE_BON between   :debut and :fin  GROUP BY IMMAT ");
  //  Query q =session.createQuery("select immat,sum(prix) as mnt from BonEssence group    by immat  ");
    q.setParameter("debut",d1  );
    q.setParameter("fin",d2  );
return q.list();
    }catch (RuntimeException e) {
        session.getTransaction().rollback();
        throw e;
    }finally{

        session.getTransaction().commit();
    }


}

private Date d1;
private Date d2;
public Date getD1() {
    return d1;
}
public void setD1(Date d1) {
    this.d1 = d1;
}
public Date getD2() {
    return d2;
}
public void setD2(Date d2) {
    this.d2 = d2;
}
public List<Object[]> getMontantParVehiculeTotal(){
    return essenceService.getMontantParVehicule(d1, d2);
    }

我的 xhtml 页面如下所示:

<h:panelGrid columns="2" id="grid2">

                <h:outputLabel value=" debut: *" for="txt_d1" />
                 <p:calendar locale="fr" value="#{bonEssenceBean.d1}" id="txt_d1" datePattern="dd/yyyy/MM" required="true" ></p:calendar>

                <h:outputLabel value=" Fin: *" for="txt_d2" />
                 <p:calendar locale="fr" value="#{bonEssenceBean.d2}" id="txt_d2" datePattern="dd/yyyy/MM" required="true" ></p:calendar>


             <p:button id="btn_add" value="Enregistrer"
                outcome="DetailBon" >
                <f:param name="d1" value="#{bonEssenceBean.d1}"></f:param>
                <f:param name="d2" value="#{bonEssenceBean.d2}"></f:param>
                </p:button>

        </h:panelGrid>

<ui:composition template="/Views/Template/common.xhtml">
  <ui:define name="Edition">
    <h:form id="form">

    </h:form>
  </ui:define>

  <ui:define name="Consultation">
  <h:form id="form2">
  <!-- ////////////////////////////////////////////////////////////////////////////     -->

  <p:dataTable id="statTable" var="item" value="#{bonEssenceBean.montantParVehiculeTotal}"  widgetVar="statTable" sortMode="single" rows="5" paginator="true" paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" rowsPerPageTemplate="5,15,25">    
  <f:facet name="header">
          <p:outputPanel>  
               <h:outputText value="Search:" style="Height:30px"/>  
               <p:inputText id="globalFilter" onkeyup="carsTable.filter()"  style="width:150px" />  
          </p:outputPanel>  

       </f:facet>
        <p:column  headerText="Vehicule"   > 

            <h:outputText value="#{item[0]}" />

        </p:column>  

        <p:column  headerText="COUT">  
            <h:outputText value="#{item[1]}" />  
        </p:column>  
        <p:column  headerText="QUANTITE">  
            <h:outputText value="#{item[2]}" />  
        </p:column> 
         <p:column  headerText="Nombre de bon">  
            <h:outputText value="#{item[3]}" />  

        </p:column>



    </p:dataTable>



  </h:form>
  </ui:define>
</ui:composition>
</html>

我认为这是因为我的参数没有正确传递给 SQL 请求。有人可以帮我吗?

4

1 回答 1

1

数据访问层

如果出现异常(无效的 HQL,可能基于null过滤器参数),您将回滚事务,但稍后您总是finally在块中提交事务。这可能会产生另一个异常,第一个异常丢失。

伪代码:

try {
  session.beginTransaction();
  // HQL
  session.getTransaction().commit();
} catch (Exception ex) {
  session.getTransaction().rollback();
  throw e;
}

表示访问层

这个enterDate.xhtml

<h:form id="form1">
    <h:panelGrid columns="2">
        <h:outputLabel value="Date 1" for="date1" />
        <h:inputText value="#{dateModel.date1}" id="date1">
            <f:convertDateTime pattern="dd.MM.yyyy" />
        </h:inputText>

        <h:outputLabel value="Date 2" for="date2" />
        <p:calendar locale="de" value="#{dateModel.date2}" id="date2"
            required="true" />
    </h:panelGrid>

    <p:commandButton id="button1" value="Show" action="showDate" />
</h:form>

和...一起showDate.xhtml

<h:outputText value="#{dateModel.date1}" />
<br />
<h:outputText value="#{dateModel.date2}" />

使用这个模型

@ManagedBean(name = "dateModel")
@SessionScoped
public class DateModel {
    private Date date1 = new Date();
    private Date date2 = new Date();

    public Date getDate1() {
        return this.date1;
    }

    public void setDate1(Date _date1) {
        this.date1 = _date1;
        System.out.println("date1: " + this.date1);
    }

    public Date getDate2() {
        return this.date2;
    }

    public void setDate2(Date _date2) {
        this.date2 = _date2;
        System.out.println("date2: " + this.date2);
    }
}

显示日期。

于 2013-08-13T09:16:39.693 回答