gpt4 book ai didi

java - 异常 java.lang.ClassCastException

转载 作者:行者123 更新时间:2023-12-01 14:46:52 28 4
gpt4 key购买 nike

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

最佳答案

考虑以下三个示例。

  • 如果您尝试选择表格的所有列。然后你必须像下面的代码一样编写你的程序。

例如

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];
...
}

关于java - 异常 java.lang.ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15353419/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com