0

我试图从表中只读取两列(一列是整数,另一列是字符串)并获取结果列表。我正在阅读表格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;
    }

}

然后在托管 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);
}
4

1 回答 1

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];
            ...
        } 
于 2013-03-12T04:55:27.350 回答