2017-04-26 27 views
0

我面對試圖調用在啊顯示的列表中的每一項bean方法時,一個問題一個bean的方法:數據表,我得到的folwing例外:呼籲每個數據表項

- INFOS: Exception when handling error trying to reset the response. javax.persistence.NoResultException: No entity found for query 
at org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:498) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 

我有一個帳戶實體和部門實體,它們有一個名爲代碼的公共列,但它們沒有鏈接,因此爲了顯示與帳戶相同代碼的部門,我使用此共享屬性(代碼)根據帳戶的代碼獲取部門。

<h:form prependId="false"> 
    <h:dataTable id="accounts_list" class="table table-bordered table-striped table-hover table-condensed" 
    var="account" value="#{AccountBean.accountProvider.allAccounts}"> 
     <h:column> 
     <f:facet name="header"> 
      <h:outputText value="account type"/> 
     </f:facet> 
     <h:outputText value="#{account.categoy.label}"/> 
     </h:column> 
     <h:column> 
     <f:facet name="header"> 
      <h:outputText value="account code"/> 
     </f:facet> 
     <h:outputText value="#{account.code}"/> 
     </h:column> 
     <h:column> 
     <f:facet name="header"> 
      <h:outputText value=" department"/> 
     </f:facet> 
     <h:outputText value="#{AccountBean.searchDepByAccountCode(account.code)}"/> 
     </h:column> 
         ..... 
    </h:dataTable> 
</h:form> 

是否可以繼續這樣做,或者我應該尋找另一種解決方案來做到這一點。

謝謝,

+0

這是業務邏輯的一部分。如果沒有這樣的部門,你應該怎麼做?你可以例如捕獲異常並在searchDepByAccountCode()方法中返回空字符串或「not found」字符串。你應該先決定。 – StanislavL

+0

謝謝,這是問題的理念,我已經檢查使用SQL,但似乎像數據改變,因爲,通過一個簡單的嘗試趕上解決了這個問題 – abbr

回答

0

這是業務邏輯的一部分。如果沒有這樣的部門,你應該怎麼做?你可以例如捕獲異常並在searchDepByAccountCode()方法中返回空字符串或「not found」字符串。

或者,您可以返回不是單個結果,而是一個detaartment列表。該列表可以是空的,包含一個或多個項目。