交易未成功启动

时间:2013-08-13 08:57:53

标签: hibernate jsf-2 transactions

我正在尝试将日期参数传递给我的应用程序,但我有一些困难 我的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请求。有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

数据访问层

如果出现异常(无效的HQL,可能基于null过滤器参数),您将回滚该事务,但稍后您始终以{{1}提交事务阻止。这可能会造成另一个例外,第一个就会丢失。

伪代码:

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}" />

显示日期。