0

テーブルから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);
}
4

1 に答える 1

1

以下の3つの例を考えてみましょう。

  • テーブルのすべての列を選択しようとしている場合。次に、以下のコードのようにプログラムを作成する必要があります。

例えば

Query q = em.createQuery("SELECT t FROM BusinessProcessTable t");    
List<BusinessProcessTable > result = q.getResultList();
  • テーブルのすべての列を1つ取得しようとしている場合。次に、以下のコードのようにプログラムを作成する必要があります。

例えば

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];
            ...
        } 
于 2013-03-12T04:55:27.350 に答える