2011-02-24 12 views
0

我想刪除與當前影片不匹配的評論;如何使用當前上下文刪除項目集合JPA,JSF,Java Collections

信息

  jpaController = (CommentFacade) facesContext.getApplication().getELResolver().getValue(facesContext.getELContext(), null, "commentJpa"); 
     movieController = (MovieController) facesContext.getApplication().evaluateExpressionGet(facesContext, "#{movie}", MovieController.class); 
     private List<Comment> userCommentItems = null; 
     private Comment userComment = null; 

public List<Comment> getUserCommentItems() { 
    if (userCommentItems == null) { 
     getUserPagingInfo(); 
     Vector comments = (Vector) jpaController.findAll(); 
     Vector v = new Vector(comments); 
     for (Iterator iterator = comments.listIterator(); iterator.hasNext();){ 
       userComments = (Comment) iterator.next(); 
       if (userComments.getMovie().getIdMovie() != movieController.getMovie().getIdMovie()){ 
        v.remove(userComments); 
       } 
      } 
     userCommntItems = v; 
    } 
    return userCommentItems ; 
} 


    <h:panelGroup> 
     <h:outputText value="Item #{comment.userPagingInfo.firstItem + 1}..#{comment.userPagingInfo.lastItem} of #{comment.userPagingInfo.itemCount}"/>&nbsp; 
     <h:commandLink action="#{comment.userPrev}" value="Previous #{comment.userPagingInfo.batchSize}" 
    rendered="#{comment.userPagingInfo.firstItem >= comment.userPagingInfo.batchSize}"/> 
    <h:commandLink action="#{comment.userNext}" value="Next #{comment.userPagingInfo.batchSize}" rendered="#{comment.userPagingInfo.lastItem + comment.userPagingInfo.batchSize <= comment.userPagingInfo.itemCount}"/>&nbsp; 
    <h:commandLink action="#{comment.userNext}" value="Remaining #{comment.userPagingInfo.itemCount - comment.userPagingInfo.lastItem}" 
          rendered="#{comment.userPagingInfo.lastItem < comment.userPagingInfo.itemCount && comment.userPagingInfo.lastItem + comment.userPagingInfo.batchSize > comment.userPagingInfo.itemCount}"/> 

    <h:dataTable value="#{comment.userCommentItems}" var="item" 
      border="0" cellpadding="2" cellspacing="0" rowClasses="jsfcrud_odd_row,jsfcrud_even_row" rules="all" style="border:solid 1px" 
      rendered="#{not empty comment.movieController.movie.commentCollection}"> 

    <h:column> 
     <f:facet name="header"> 
      <h:outputText value="IdUser"/> 
     </f:facet> 
     <h:outputText value="#{item.idUser.user}"/> 
    </h:column> 
    <h:column> 
     <f:facet name="header"> 
      <h:outputText value="Text"/> 
     </f:facet> 
     <h:outputText value="#{item.text}"/> 
    </h:column> 
</h:dataTable> 
</h:panelGroup> 
+1

你得到的錯誤是什麼? –

+0

沒有錯誤,但爲空我得到一個空的數據表 –

+0

沒有抱歉我現在正在收到所有評論,所有電影的評論。我的意思是,一旦評論結束,他們對於我導航的所有電影都保持不變。 –

回答

2

首先,這是可怕的代碼。這個版本怎麼樣?

public List<Comment> getUserCommentItems() { 
    if (userCommentItems == null) { 
     getUserPagingInfo(); 
     List<Comment> comments = jpaController.findAll(); 
     for (Iterator<Comment> iterator = comments.iterator(); iterator.hasNext();){ 
       userComments = iterator.next(); 
       if (!userComments.getMovie().equals(movieController.getMovie()){ 
        iterator.remove(); 
       } 
      } 
     userCommntItems = comments; 
    } 
    return userCommentItems ; 
} 

改進:

  • 沒有更多的使用過時的Vector類(尤其是沒有更多的不必要的強制類型轉換)
  • 使用equals()比較對象,而不是==(你應該實現Movie.equals(Object)相應)。
  • 無需複製收集,工作,對原來的(如果你的DAO返回的東西,我不能那麼它吮吸更改)

但要實際解決問題:

是您是否將這個組件用於不同的電影?如果是這樣,保持在一個字段中的評論是無稽之談。刪除userCommentItems的所有分配和讀取。

+0

metod在類java.util.iterator中刪除不能應用於給定類型所需:沒有找到參數:myBeans –

+0

@Ignacio對不起,我的壞,它不需要參數。它刪除迭代中的當前元素(現在是固定代碼) –

+0

這是唯一的賦值和從userCommentItems讀取 –