2011-06-21 53 views
0

我正在使用JPA/Hibernate 我想知道如何使用createquery來檢索列表。 我曾嘗試:如何使用對象查詢語言從查詢中獲取結果列表

EntityManagerFactory emf=Persistence.createEntityManagerFactory("JiraMapPUKPI"); 
EntityManager em= emf.createEntityManager(); 
EntityTransaction entr=em.getTransaction(); 
entr.begin(); 
Query multipleSelect =em.createQuery("select distinct k.kpiName FROM Kpi k ,ProfilUser p ,AssociationKpiProfil a WHERE p.profilUserId= :w and p.profilUserId= a.profilUser and a.kpi=k.kpiId ").setParameter("w", userid); 

但後來我不能使用:

List userList = (List)multipleSelect.getResultList(); 

因爲multipleSelect不包含getResultList方法

我試圖也:

javax.persistence.Query multipleSelect= em.createQuery("select k.kpiName FROM Kpi k ,ProfilUser p ,AssociationKpiProfil a WHERE p.profilUserId= :w and p.profilUserId= a.profilUser and a.kpi=k.kpiId "); 
    multipleSelect.setParameter("w", userid); 
    List usrpro= multipleSelect.getResultList(); 

但這出現錯誤:

EL Warning]: 2011-06-21 08:53:08.81--UnitOfWork(6850900)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.DatabaseException 
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have  an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')) AND (t3.KPI_ID = t0.KPI_ID.t0.KPI_ID)) AND (t2.PROFIL_USER_ID = t3.PROFIL_ID)' at line 1 
Error Code: 1064 
Call: SELECT t0.KPI_NAME FROM association_kpi_profil t3, profil_user t2, profil_user , kpi t0 WHERE ((((t1.PROFIL_USER_ID = ?) AND (t1.PROFIL_USER_ID =)) AND (t3.KPI_ID = t0.KPI_ID.t0.KPI_ID)) AND (t2.PROFIL_USER_ID = t3.PROFIL_ID)) 
bind => [1 parameter bound] 
Query: ReportQuery(referenceClass=Kpi sql="SELECT t0.KPI_NAME FROM association_kpi_profil t3, profil_user t2, profil_user t1, kpi t0 WHERE ((((t1.PROFIL_USER_ID = ?) AND (t1.PROFIL_USER_ID =)) AND (t3.KPI_ID = t0.KPI_ID.t0.KPI_ID)) AND (t2.PROFIL_USER_ID = t3.PROFIL_ID))") 

Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')) AND (t3.KPI_ID = t0.KPI_ID.t0.KPI_ID)) AND (t2.PROFIL_USER_ID = t3.PROFIL_ID)' at line 1 
Error Code: 1064 
Call: SELECT t0.KPI_NAME FROM association_kpi_profil t3, profil_user t2, profil_user t1, kpi t0 WHERE ((((t1.PROFIL_USER_ID = ?) AND (t1.PROFIL_USER_ID =)) AND (t3.KPI_ID = t0.KPI_ID.t0.KPI_ID)) AND (t2.PROFIL_USER_ID = t3.PROFIL_ID)) 
bind => [1 parameter bound] 
Query: ReportQuery(referenceClass=Kpi sql="SELECT t0.KPI_NAME FROM association_kpi_profil t3, profil_user t2, profil_user t1, kpi t0 WHERE ((((t1.PROFIL_USER_ID = ?) AND (t1.PROFIL_USER_ID =)) AND (t3.KPI_ID = t0.KPI_ID.t0.KPI_ID)) AND (t2.PROFIL_USER_ID = t3.PROFIL_ID))") 
errrrrs 
list kpiiiiiiiinull 

請問我該怎麼做? 謝謝

回答

0

由Hibernate生成的查詢不正確。 t1.PROFIL_USER_ID參數不存在。嘗試檢查您的映射,並使用JOIN而不是等號。

+0

你好,錯誤是在我的SQL請求:),而不是p.profilUserId = a.profilUser它應該是p = a.profilUser – ramram