2014-11-14 91 views
0

我收到類轉換異常 作爲java.lang.ClassCastException:com.model.Image不能轉換爲java.lang.String

java.lang.ClassCastException:com.model.Image不能轉換爲 java.lang.String中

下面是我的功能由一個實體類的JPA

public List<Image> complete(String fileName) 
{ 
    List<Image> queryResult = new ArrayList<Image>(); 
    System.out.println("File Name is : "+fileName); 
    if (allImages == null) 
    { 
     System.out.println("Yeah I am coming here"+allImages);   
     allImages = imageFacade.findAll(); 
    }  
    allImages.removeAll(servicesWithImage.getImage()); 
    for (Image image : allImages) 
    { 
     if (image.getFileName().toLowerCase().contains(fileName.toLowerCase())) 
     { 
      queryResult.add(image); 
     } 
    } 

    return queryResult; 
} 
創建MySQL表獲取數據

我已經定義了allImages全局作爲

private List<Image> allImages; 

上面完整方法我從Primefaces的p:autoComplete

<p:autoComplete forceSelection="true" 
       minQueryLength="3" 
       value="#{servicesMB.image}" 
       label="#{msgs.image}" 
       required="true" 
       var="image" 
       itemLabel="#{image.fileName}" 
       itemValue="#{image}" 
       completeMethod="#{servicesMB.complete}" 
       dropdown="true" /> 

調用完整方法的的findAll方法如下所示:

public List<T> findAll() 
{ 
    CriteriaQuery cq = em.getCriteriaBuilder().createQuery(); 
    cq.select(cq.from(entityClass)); 
    return em.createQuery(cq).getResultList(); 
} 

如果我把Print Statement寫成完整的方法,我得到了所有需要的數據bu t對數據沒有在p:autoComplete顯示,並且還我收到ClassCastException異常

我將編輯我完成上述方法有哪些東西看起來像

public List<Image> complete(String fileName) 
{ 
    List<Image> queryResult = new ArrayList<Image>(); 
    if (allImages == null) 
    { 
     System.out.println("I am in If Condition"); 
     allImages = imageFacade.findAll(); 
     System.out.println("The Value of allImages : "+allImages); 
    } 
    System.out.println("Getter function servicesWithImage"+servicesWithImage.getImage()); 
    allImages.removeAll(servicesWithImage.getImage()); 
    System.out.println("allImages after removeAll method"+allImages); 

    for (Image image : allImages) 
    { 

     if (image.getFileName().toLowerCase().contains(fileName.toLowerCase())) 
     { 
      queryResult.add(image); 
     } 
    } 
    System.out.println("At the End The Value of queryResult"+queryResult); 
    return queryResult; 
} 

我的輸出(僅用於方法)打印報表:

02:58:56,104 INFO [stdout] (http-localhost-127.0.0.1-8080-5) I am in If Condition 
02:58:56,121 INFO [stdout] (http-localhost-127.0.0.1-8080-5) The Value of allImages : [services_2.jpg, services_3.jpg] 
02:58:56,122 INFO [stdout] (http-localhost-127.0.0.1-8080-5) Getter function servicesWithImage[] 
02:58:56,122 INFO [stdout] (http-localhost-127.0.0.1-8080-5) allImages after removeAll method[services_2.jpg, services_3.jpg] 
02:58:56,123 INFO [stdout] (http-localhost-127.0.0.1-8080-5) At the End The Value of queryResult[services_2.jpg, services_3.jpg] 
02:58:56,130 SEVERE [javax.enterprise.resource.webcontainer.jsf.context] (http-localhost-127.0.0.1-8080-5) 
java.lang.ClassCastException: com.model.Image cannot be cast to java.lang.String 

幫助...

回答

1

可能是由於自動完成。

自動完成就像選擇,它本身不能管理對象,需要一個轉換器,而這個轉換器必須能夠將圖像對象轉換爲一個字符串,並再次將該字符串轉換爲圖像。

這裏你可以看到詳細的解釋https://stackoverflow.com/a/8001418/4253629]1

+0

花了更多的時間來理解解決方案,但是正確的解決方案非常感謝 – Zaheer 2014-11-15 13:35:38

相關問題