Coldfusion,Coldbox,Hibernate:使用Subselect查询

时间:2013-11-20 18:00:33

标签: sql hibernate coldfusion coldbox

我在一个项目上遇到了麻烦,我正在向社区寻求帮助。

我将在前言中说我对ColdFusion不是很熟悉(我在12年前对它进行了一些讨论,我知道它从那时起就已经发展了)而且我对Coldbox一点也不熟悉或者Hibernate。

我有一个Coldbox处理程序(如下所示),它从数据库中提取事件实体列表并以JSON格式返回。目前,这工作正常(问题区域之外的一切)。但是,我们现在需要通过另一个名为Quantifications的表来过滤事件列表。基本上,我需要匹配a)与它们相关的量化的所有事件,以及b)至少有一个状态为“活动”的量化。

我有几种合理的方法可以解决这个问题。第一个(和非理想的)是简单地获取我的所有结果,然后在一组循环中过滤它们。然而,这是昂贵的并且会混乱分页,所以我抛弃了这个想法。

第二个是在Quantifications表上进行连接,返回与它们对应的所有活动量化和事件。但是,我遇到了重复,Coldbox的resultTransformer实际上似乎没有工作 - DISTINCT_ROOT_ENTITY没有返回不同的实体。 (我也严重缺乏代码引用,而且我没有办法轻松地逐步完成代码。)

我的最终想法是 - 对于每个事件,执行与事件相关的量化量化表的子查询,然后计算有多少事件的计数。如果计数为正,则返回事件。

如何使用Coldbox和Hibernate实现这一功能?我知道我想做什么(以及如何在逻辑上解决问题),但我对这个系统中如何使用这些工具以及这种语法知之甚少,我很遗憾没有时间去理解它出。谢谢!

<cfscript>
component extends="handlers._jsonApi" {

    property name='statusService' inject='entityService:EventStatus';
    property name='departmentService' inject='entityService:Department';
    property name='myEventService' inject='id:myEventService';
    property name='quantificationService' inject='entityService:Quantification';

    ///api/v1/events?departmentId=[XXX]&date=[2013-08-02]&showEventsWithoutQuantifications[false|true]
    function index(event,rc,prc){

        var myEventCriteria = myEventService.newCriteria();

        //if a date is passed, display events occuring that date
        if (StructKeyExists(rc, "date")){
            if (isDate(rc.date)){
                var date = createODBCDateTime(rc.date);
            } else {
                addMessage("The date passed (#rc.date#) is invalid. The date must be in the following format: YYYY-MM-DD.", "error");
                return;
            }

        } else {//else display events occurring today

            var date = createODBCDateTime(now());

        }

        myEventCriteria.le("startDate", date);
        myEventCriteria.ge("endDate", date);

        //if a department id is passed, restrict events to that department
        if (( StructKeyExists(rc, "departmentId")) AND (len(rc.departmentId))){

            var departmentId = rc.departmentId;
            var department = departmentService.findWhere(entityName="Department", criteria={departmentId=departmentId});

            if (isNull(department)){
                addMessage("'#rc.departmentId#' is not a valid departmentId.", "error");
                return;         
            }

            myEventCriteria.isEq("department", department);
        }

        var showEventsWithoutQuantifications = false;
        if (StructKeyExists(rc, "showEventsWithoutQuantifications")){
            if (rc.showEventsWithoutQuantifications)    showEventsWithoutQuantifications = true;
        }

        if (NOT showEventsWithoutQuantifications){
            myEventCriteria.isNotEmpty('quantifications');
        }

/* PROBLEM AREA */
        // Filter events to only include those that have approved quantifications.
        var statusId = 150;
        var status = statusService.findWhere(entityName="EventStatus", criteria={eventStatusOrder=statusId});

        myEventCriteria.createAlias("quantifications", "qt").isEq("qt.status", status);
        myEventCriteria.withProjections(countDistinct="eventID");

        myEventCriteria.resultTransformer( myEventCriteria.DISTINCT_ROOT_ENTITY );

/* // PROBLEM AREA */

        var recordCount = myEventCriteria.count();

        //the order needs to be set after the count is retrieved, or it returns an error
        //myEventCriteria.order('startDate', 'asc');
        myEventCriteria.order('eventID', 'asc');

        var pageSize = getSetting("defaultPageSize");
        if (StructKeyExists(rc, "pageSize") AND Len(rc.pageSize))   pageSize = rc.pageSize;

        myEventCriteria.maxResults(pageSize);

        if (recordCount GT pageSize){

            var firstResult = 0;
            var pageNumber = 1;
            var pageCount = Ceiling(recordCount / pageSize);
            if (StructKeyExists(rc, "pageNumber") AND Len(rc.pageNumber)){
                pageNumber = rc.pageNumber;
                if (pageNumber LT 0 OR pageNumber GT pageCount){
                    addMessage("The pageNumber must be between 1 and #pageCount#", "error");
                    return;
                }

                firstResult = (pageNumber - 1) * pageSize;
                myEventCriteria.firstResult(firstResult);
            }

            var paging = StructNew();
            paging['pageSize'] = pageSize;
            paging['pageNumber'] = pageNumber;
            paging['pageCount'] = pageCount;
            paging['recordCount'] = recordCount;

            prc.response['paging'] = paging;
        }

        prc.response['events'] = objectsToStructs(myEventCriteria.list(), "eventID,eventName,eventCity,eventState,eventVenue,startDate,endDate,justfication,department.departmentID,department.departmentName,country.countryID,country.countryCode,country.countryName,country.countryRegion,signInSummary.signInSummaryID,signInSummary.signInSummary,serviceType.serviceTypeID,serviceType.serviceTypeName" );

    }//end index


}//end component

</cfscript>

0 个答案:

没有答案