如何在我的JPQL查询中使用UNION和Order by Clause

时间:2015-03-05 06:24:57

标签: eclipselink

我在项目中使用eclipse链接作为JPA。我正在使用的当前jar是eclipselink-2.5.2.Coming to my Question,  我想在UNION组合中使用Order by。我的JPQL查询如下所示

select req from Rqst req  
 WHERE (req.Applc =:SYSTEM_IDENTIFIER1 AND req.procesTyp  =:PROCESS_TYPE1 AND UPPER(req.updtBy) =:UPDATED_BY1) 
 AND req.stat <> :ignoreDeletedRequests 
UNION 
select req from Rqst req 
 WHERE (req.Applc =:SYSTEM_IDENTIFIER2 AND req.procesTyp  =:PROCESS_TYPE2 AND UPPER(req.updtBy) =:UPDATED_BY2) 
 AND req.stat <> :ignoreDeletedRequests 
UNION 
select req from Rqst req 
 WHERE (req.Applc =:SYSTEM_IDENTIFIER3 AND req.procesTyp  =:PROCESS_TYPE3 AND UPPER(req.updtBy) =:UPDATED_BY3) 
 AND req.stat <> :ignoreDeletedRequests 
 ORDER BY req.rqstId ASC

有了它的顺序,它对我有用。但是当我使用Order时,我得到的查询包含一个格式错误的结尾。

我能在这里帮助我很长时间。

1 个答案:

答案 0 :(得分:0)

我遇到了问题。我通过使用Collection.sort()解决了这个问题:

Collections.sort(listObjects, new Comparator<Object[]>() {

                    @Override
                    public int compare(final Object[] record1, final Object[] record2) {
                        int c;
                        c = convertTime(record1[0].toString()).compareTo(convertTime(record2[0].toString()));
                        if (c == 0) {
                            c = record1[1].toString().compareTo(record2[1].toString());
                        }
                        if (c == 0) {
                            c = record1[2].toString().compareTo(record2[2].toString());
                        }
                        if (c == 0) {
                            c = new Long(Long.parseLong(record1[3].toString())).compareTo(Long.parseLong(record2[3].toString()));
                        }
                        if (c == 0) {
                            c = ((Date) record1[4]).compareTo(((Date) record2[4]));
                        }
                        if (c == 0) {
                            c = ((Date) record1[5]).compareTo(((Date) record2[5]));
                        }
                        if (c == 0) {
                            c = new Long(Long.parseLong(record1[6].toString())).compareTo(Long.parseLong(record2[6].toString()));
                        }
                        return c;
                    }

                });

您可以使用Collention.sort()来订购多个字段;