2013-03-12 44 views
0

我想從表中只讀取兩列(一個是整數,其他是字符串),並得到結果列表。我正在使用EntityManger來讀取表格,如下面的代碼所示。此代碼正確執行並且不會出現異常。異常java.lang.ClassCastException

@SuppressWarnings("unchecked") 
public List<BusinessProcessIdAndName> getBusinessProcessList(int level) { 
    List<BusinessProcessIdAndName> businessProcessTableList = new  ArrayList<BusinessProcessIdAndName>(); 
    EntityManager em = null; 
    try { 
     EntityManagerFactory emf = Persistence.createEntityManagerFactory(ApplicationConstants.DERBY_PERSISTENCE_UNIT_NAME); 
     em = emf.createEntityManager(); 
     EntityTransaction et = em.getTransaction(); 
     et.begin(); 
     Query query = em.createQuery("select t.businessProcessId, t.businessProcessName from BusinessProcessTable t"); 
     businessProcessTableList = query.getResultList(); 
     // The line below displays the correct result, The logger is basically a System.out.println 
     logger.debug(businessProcessTableList.size()); 
    } 
    catch(Exception e) { 
     logger.debug("Exception Caught while getting BP List: " + e); 
    } 
    return businessProcessTableList; 
} 

BusinessProcessIdAndName類是如下

public class BusinessProcessIdAndName { 

    private Integer businessProcessId; 
    private String businessProcessName; 

    public Integer getBusinessProcessId() { 
     return businessProcessId; 
    } 

    public void setBusinessProcessId(Integer businessProcessId) { 
     this.businessProcessId = businessProcessId; 
    } 

    public String getBusinessProcessName() { 
     return businessProcessName; 
    } 

    public void setBusinessProcessName(String businessProcessName) { 
     this.businessProcessName = businessProcessName; 
    } 

} 

然後,在管Bean我使用如下上述代碼的結果。這裏我得到異常

java.lang.ClassCastException:[Ljava.lang.Object;與 com.ewt.ewtalmutil.object.BusinessProcessIdAndName不相容

我知道這個異常說有是對象不匹配,他們是不兼容的,但是我覺得我的對象應該是兼容的,請告訴我在哪裏我錯了,做什麼是正確的方法。

public SelectCriteriaBean() { 
    logger.entering(CLASS_NAME); 
    this.businessProcessLevelZeroList = new ArrayList<SelectItem>(); 
    List<BusinessProcessIdAndName> tempBusinessProcessLevelZeroList = new BusinessProcessTableManager().getBusinessProcessList(0); 
    // The line below also displays correct result 
    logger.debug(tempBusinessProcessLevelZeroList.size()); 
    // The line below gives Exception: java.lang.ClassCastException: [Ljava.lang.Object; incompatible with com.ewt.ewtalmutil.object.BusinessProcessIdAndName 
    try { 
     Iterator<BusinessProcessIdAndName> iterator = tempBusinessProcessLevelZeroList.iterator(); 
    } 
    catch (Exception e) { 
     logger.debug("Exception: " + e); 
    } 
    while (iterator.hasNext()) { 
     BusinessProcessIdAndName businessProcessIdAndName = new BusinessProcessIdAndName(); 
     businessProcessIdAndName = iterator.next(); 
     // The Exception/Error comes in the line below 
     String businessProcessName = businessProcessIdAndName.getBusinessProcessName(); 
     int businessProcessId = businessProcessIdAndName.getBusinessProcessId(); 
     String businessProcessIdString = String.valueOf(businessProcessId); 
     SelectItem item = new SelectItem(businessProcessIdString,businessProcessName); 
     businessProcessLevelZeroList.add(item); 
    } 
    setBusinessProcessLevelOneListRendered(false); 
    setAddBusinessProcessRendered(false); 
    logger.exiting(CLASS_NAME); 
} 
+2

你有兩條線評論道給予例外。哪一個?如果第一個,記錄器是否按照'catch'子句'預期的那樣記錄異常,或者程序是否拋出異常?你確定你正確地計算行數嗎? – 2013-03-12 04:43:48

+0

您創建迭代器的行不會引發該異常。在將'businessProcessIdAndName'分配給'iterator.next()'之前,初始化行是浪費時間和空間。 – EJP 2013-03-12 04:50:17

+0

異常來自try catch塊,所以代碼行businessProcessIdAndName = iterator.next();給出例外 – SXV 2013-03-12 05:00:09

回答

1

考慮下面的三個例子。

  • 如果您嘗試選擇表中的所有列。然後你必須像下面的代碼一樣編寫你的程序。

Query q = em.createQuery("SELECT t FROM BusinessProcessTable t");  
List<BusinessProcessTable > result = q.getResultList(); 
  • 如果你想獲得單一的,而是一個表的所有列。然後你必須像下面的代碼一樣編寫你的程序。

Query q1 = em.createQuery("SELECT t FROM BusinessProcessTable t WHERE t.id = :id"); 
q1.setParameter("id", "4711"); 
BusinessProcessTable e = (BusinessProcessTable)q1.getSingleResult(); 
  • 如果你想獲得一個表的選擇性列的所有記錄,則返回類型將列出的對象。你應該像下面的代碼一樣編寫你的程序。

Query q1 = em.createQuery("SELECT t.name, t.salary FROM BusinessProcessTable t"); 
     List<Object[]> result1 = q1.getResultList(); 
     for (Object[] resultElement : result1) { 
      String name = (String)resultElement[0]; 
      Double salary = (Double)resultElement[1]; 
      ... 
     } 
相關問題