简体   繁体   中英

DISTINCT query on multiple columns doesn't work - Google App engine Datastore

I want to fetch distinct multiple columns from Google app engine Datastore in endpoint class. For that i am using below code but problem is that if i try to fetch only single column then only it works properly and returns DISTINCT data. If i try to fetch multiple columns then DISTINCT doesn't work and it returns all records without any effect of Distinct query. I don't know what is wrong with my code. Please suggest any solution and guide me where i am going wrong. Thank you.

Code:

@SuppressWarnings({ "unchecked" })
    @ApiMethod(name = "getDistinctFollow", httpMethod = HttpMethod.GET, path = "userendpoint/userName_fk3")
    public ObjectListContainer getDistinctFollower(
            @Named("followName") ArrayList<String> lstFollower,
            @Nullable @Named("cursor") String cursorString,
            @Nullable @Named("limit") Integer limit) {

        EntityManager mgr = null;
        Cursor cursor = null;
        List<Object[]> lstNames;
        List<String> lstString;
        ObjectListContainer object;
        try {
            mgr = getEntityManager();
            Query query = mgr.createQuery("select distinct f.uFullName,f.uUrl from UMaster f where f.uName in (:followName)");
            query.setParameter("followName", lstFollower);
            if (cursorString != null && cursorString != "") {
                cursor = Cursor.fromWebSafeString(cursorString);
                query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
            }
            if (limit != null) {
                query.setFirstResult(0);
                query.setMaxResults(limit);
            }
            lstNames = (List<Object[]>) query.getResultList();
            cursor = JPACursorHelper.getCursor(lstNames);
            if (cursor != null)
                cursorString = cursor.toWebSafeString();

            lstString = new ArrayList<String>();

            for (Object obj[] : lstNames) {
                lstString.add(obj != null ? obj[0].toString() : null);
                lstString.add(obj != null ? obj[1].toString() : null);
            }
            object = new ObjectListContainer();
            object.setObjectsList(lstString);

        } finally {
            mgr.close();
        }
        return object;
    }

Notice that DISTINCT only works with Indexed properties. Is any of uFullName or uName are not indexed, then the projection won't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM