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);
}
你有兩條線評論道給予例外。哪一個?如果第一個,記錄器是否按照'catch'子句'預期的那樣記錄異常,或者程序是否拋出異常?你確定你正確地計算行數嗎? – 2013-03-12 04:43:48
您創建迭代器的行不會引發該異常。在將'businessProcessIdAndName'分配給'iterator.next()'之前,初始化行是浪費時間和空間。 – EJP 2013-03-12 04:50:17
異常來自try catch塊,所以代碼行businessProcessIdAndName = iterator.next();給出例外 – SXV 2013-03-12 05:00:09