简体   繁体   中英

Exception java.lang.ClassCastException

I am trying to read only two column (One is Integer and other is String) from the table and get a list of the result. I am reading the table using EntityManger as shown in the code below. This code executes correctly and no Exception comes.

@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 class is as below

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

}

Then in Managed Bean I am using the result of the above code as below. Here I get the Exception

java.lang.ClassCastException: [Ljava.lang.Object; incompatible with com.ewt.ewtalmutil.object.BusinessProcessIdAndName

I know this Exception says that there is a mismatch in the object and they are incompatible, But I think my objects should be compatible, please tell me where I am wrong and what is the correct way to do it.

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

Consider below three example.

  • If you are trying to select all the column of a table. Then you have to write your program like below code.

Eg

Query q = em.createQuery("SELECT t FROM BusinessProcessTable t");    
List<BusinessProcessTable > result = q.getResultList();
  • If you are trying get single but all the column of a table. Then you have to write your program like below code.

Eg

Query q1 = em.createQuery("SELECT t FROM BusinessProcessTable t WHERE t.id = :id");
q1.setParameter("id", "4711");
BusinessProcessTable e = (BusinessProcessTable )q1.getSingleResult();
  • If you are trying to get all the records of selective column of a table then the return type would list of objects. You should write your program like below code.

Eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM