JPQL EclipseLink错误联合选择

时间:2017-02-23 17:13:02

标签: java jpa eclipselink

我正在尝试从有权访问和部门文件的用户那里获取不同的文件名。当我单独运行它时,两个查询都是这样的:

Query q12 = CpabeDAO.em.createQuery("SELECT f.realName FROM FileObject f JOIN f.listUserAccessor d WHERE d.id=:userID");
        q12.setParameter("userID", 401);
        List<String> res = q12.getResultList();
        for(String a : res){
            System.out.println(a);
        }
        Query q13 = CpabeDAO.em.createQuery("SELECT DISTINCT f.realName FROM FileObject f JOIN f.listDeptAccessor d WHERE d.departmentId=:deptID");
        q13.setParameter("deptID", 1);
        List<String> res2 = q13.getResultList();
        for(String a : res2){
            System.out.println(a);
        }

但是当我尝试使用UNION组合两个查询时,正如here所解释的那样,如下所示:

Query q12 = CpabeDAO.em.createQuery("SELECT f.realName FROM FileObject f JOIN f.listUserAccessor d WHERE d.id=:userID UNION SELECT a.realName FROM FileObject a JOIN a.listDeptAccessor w WHERE w.departmentId=:deptID");
        q12.setParameter("userID", 401);
q12.setParameter("deptID", 1);
        List<String> res = q12.getResultList();
        for(String a : res){
            System.out.println(a);
        }

它给了我错误如下:

Exception in thread "main" javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.4.v20160829-44060b6): 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 'UNION (SELECT DISTINCT t3.REALNAME FROM DEPT_FILE_ACCESS t5, DEPARTMENT t4, FILE' at line 1
Error Code: 1064
Call: (SELECT t0.REALNAME FROM FILEOBJECT t0, USER_FILE_ACCESS t2, USER t1 WHERE ((t1.ID = ?) AND ((t2.fileHasAccess_ID  = t0.ID) AND (t1.ID = t2.user_id))) UNION (SELECT DISTINCT t3.REALNAME FROM DEPT_FILE_ACCESS t5, DEPARTMENT t4, FILEOBJECT t3 WHERE ((t4.DEPARTMENTID = ?) AND ((t5.fileHasAccess_ID  = t3.ID) AND (t4.DEPARTMENTID = t5.dept_id)))))
    bind => [401, 1]
Query: ReportQuery(referenceClass=FileObject sql="(SELECT t0.REALNAME FROM FILEOBJECT t0, USER_FILE_ACCESS t2, USER t1 WHERE ((t1.ID = ?) AND ((t2.fileHasAccess_ID  = t0.ID) AND (t1.ID = t2.user_id))) UNION (SELECT DISTINCT t3.REALNAME FROM DEPT_FILE_ACCESS t5, DEPARTMENT t4, FILEOBJECT t3 WHERE ((t4.DEPARTMENTID = ?) AND ((t5.fileHasAccess_ID  = t3.ID) AND (t4.DEPARTMENTID = t5.dept_id)))))")
    at org.eclipse.persistence.internal.jpa.QueryImpl.getDetailedException(QueryImpl.java:382)
    at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:260)
    at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:473)
    at cpabe.service.UserRegistration.main(UserRegistration.java:351)

我的查询有问题吗?

0 个答案:

没有答案