0

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);
}
3
  • 2
    You have two lines commented as giving the exception. Which one is it? If the first, does the logger log the exception as expected from the catch clause`, or does the program throw an exception? Are you sure that you counted lines correctly? Commented Mar 12, 2013 at 4:43
  • The line where you create the iterator doesn't throw that exception. The initialization of businessProcessIdAndName the line before you assign it to iterator.next() is a waste of time and space. Commented Mar 12, 2013 at 4:50
  • Exception comes in try catch block, so the code line businessProcessIdAndName = iterator.next(); gives exception Commented Mar 12, 2013 at 5:00

1 Answer 1

1

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];
            ...
        } 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.