异常java.lang.ClassCastException

时间:2013-03-12 04:38:00

标签: java jsf-2

我试图从表中只读取两列(一个是整数,另一个是String)并得到结果列表。我正在使用EntityManger阅读表格,如下面的代码所示。此代码正确执行,不会出现异常。

@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;
    }

}

然后在Managed Bean中我使用上面代码的结果如下。在这里,我得到了例外

  

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

我知道这个Exception说对象中存在不匹配并且它们不兼容,但我认为我的对象应该兼容,请告诉我我错在哪里以及正确的方法。

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);
}

1 个答案:

答案 0 :(得分: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];
            ...
        }