使用JPQL查詢數據庫,但無法使用List檢索「Report」表的行。這是我的代碼段:Java:使用列表從持久性查詢語言中檢索結果
...
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hibernate");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Query query = em.createQuery("SELECT r.title, r.company FROM Report as r");
List<Report> itemList = query.getResultList();
for (Report item : itemList)
{
System.out.println("Item: " + item.getCompany());
}
輸出是:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to maps.Report at hello.Test.main(Unknown Source)
Java結果:1
我在做什麼錯?爲什麼我不被允許進行演員?
是的,非常感謝!我還在hibernate-entity manager文檔中找到了關於標量結果的[link](http://docs.jboss.org/hibernate/entitymanager/3.6/reference/en/html/objectstate.html#d0e1152),以及如何處理它們。再次感謝 – giorgioca