如何使用对象查询语言从查询中获取结果列表

时间:2011-06-21 07:47:11

标签: java hibernate jpa

我正在使用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

请问我该怎么做? 谢谢

1 个答案:

答案 0 :(得分:0)

Hibernate生成的查询不正确。 t1.PROFIL_USER_ID参数不存在。尝试检查您的映射并使用JOIN而不是equals。