テーブルから2つの列(1つは整数、もう1つは文字列)のみを読み取って、結果のリストを取得しようとしています。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;
}
}
次に、Managed 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);
}