Reputation: 277
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);
}
Upvotes: 0
Views: 773
Reputation: 7016
Consider below three example.
Eg
Query q = em.createQuery("SELECT t FROM BusinessProcessTable t");
List<BusinessProcessTable > result = q.getResultList();
Eg
Query q1 = em.createQuery("SELECT t FROM BusinessProcessTable t WHERE t.id = :id");
q1.setParameter("id", "4711");
BusinessProcessTable e = (BusinessProcessTable )q1.getSingleResult();
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];
...
}
Upvotes: 1