【问题标题】:Grails 2.1 createCriteria issue with paramsGrails 2.1 createCriteria 参数问题
【发布时间】:2012-11-17 04:08:52
【问题描述】:

编辑-

我正在尝试返回一个投资组合列表,以及附加到我的 2 个域类中的每个投资组合的最后 5 个出版物。我得到最后一共 5 个出版物,每个列表显示全部 5 个。查询没有返回特定实例拥有的出版物。凯利的伟大想法重新回到了另一个轨道。

我在投资组合控制器中创建了方法,该方法是属于投资组合域类的出版物的 hasMany 方。

我似乎无法让投资组合列出他们自己的出版物。如果我更改 eq 投资组合,一切正常,除了每个投资组合列表显示相同的出版物。

如何加载每个作品集并列出最近 5 个出版物。这是从投资组合/列表页面呈现的部分。这可能是问题吗。我应该只从与投资组合列表操作无关的新视图呈现它吗?

grails 的新手并且已经阅读并阅读了文档,但似乎无法让参数查询正确返回。帮助

def _webList (){
    //def per = Portfolio.properties
    def portfolios = Portfolio.list(params.id)
    def results = Publication.withCriteria {

        eq('published', 'Yes')
        order('lastUpdated', 'desc')
        maxResults(5)
    }

    def reportscount = Publication.count()

    [ portfolios: portfolios, results: results, reportscount: reportscount]
}

如果需要,我可以显示 sql 日志。


编辑

以下代码是文件_webList.gsp 的全部部分。顶部 div -alert 在页面上加载,但 div 属性列表组合中的内容无法加载。使用 Kelly 的休眠条件会在 sql 日志中生成查询,但不会将结果或样式或任何内容返回到视图中??好奇怪!

<div class="alert alert-info" xmlns="http://www.w3.org/1999/html">Permissions apply to    <strong>editing</strong> publications.<br>
<div style="display: inline;"><p>Click portfolio name to read or edit publications. Total number of sites: <strong>${rsNumb}</strong> | Total number of publications:  <strong>${reportscount}</strong> </p>
</div>
</div>
<div class="property-list portfolio">
<g:each in="${portfolios}" var="portfolioInstance">
<div class="site-listing">
    <div><span class="label">Site Name:</span><g:link action="show" id="${portfolioInstance?.id }">${portfolioInstance?.portfolioName?.encodeAsHTML()}</g:link></div>
    <div><span class="label">Site Description:  </span>${portfolioInstance?.portdescrip?.encodeAsHTML() }</div>   <br>
    <div><span class="label">Site Administrator: </span>${portfolioInstance?.profile?.portfolioAdmin?.encodeAsHTML() }</div>   <br>
    <div><span class="label"> Total publications:</span><span class="badge badge-success"> ${portfolioInstance?.publications?.size()}</span> </div>
 <!-- whatever else you need here -->
 <!-- now iterate through the pubs -->
    <g:if test="${portfolioInstance?.publications}">
        <g:set var="publicationInstance" />
            <ul class="site-publication">
                 <li class="fieldcontain">
                     <span id="publications-label" class="property-label"><g:message code="portfolio.publications.label" default="Last 5 published publications:" /></span>
                         <g:each in="${portfolioInstance.publications}" var="publicationInstance">
                             ${publicationInstance?.id}
                                <span class="property-value" aria-labelledby="publications-label"><g:link controller="publication" action="show" id="${publicationInstance.id}">${publicationInstance?.encodeAsHTML()}</g:link></span>
<!-- and again whatever else you need here -->
                         </g:each>
        </g:if>
</g:each>
</div>

EDIT - 下面的 sql 日志

Hibernate: select this_.id as id5_1_, this_.version as version5_1_, this_.date_created as date3_5_1_, this_.last_updated as last4_5_1_, 
this_.portdescrip as portdesc5_5_1_, this_.portfolio_name as portfolio6_5_1_,   this_.portpublished as portpubl7_5_1_, this_.profile_id as profile8_5_1_, 
this_.status as status5_1_, 
publicatio1_.portfolio_id as portfolio5_5_3_, 
publicatio1_.id as id3_, publicatio1_.id as id2_0_, 
publicatio1_.version as version2_0_, 
publicatio1_.date_created as date3_2_0_, 
publicatio1_.last_updated as last4_2_0_, 
publicatio1_.portfolio_id as portfolio5_2_0_, 
publicatio1_.publication_content as publicat6_2_0_, 
publicatio1_.publication_name as publicat7_2_0_, 
publicatio1_.published as published2_0_, 
publicatio1_.publisheddate as publishe9_2_0_, 
publicatio1_.publishedemail as publish10_2_0_, 
publicatio1_.pubproduct_id as pubproduct11_2_0_ 
from portfolio this_ left outer join publication publicatio1_ 
on this_.id=publicatio1_.portfolio_id where (this_.status=?) 
and (publicatio1_.published=?) order by publicatio1_.last_updated desc

【问题讨论】:

    标签: grails grails-orm grails-2.0


    【解决方案1】:

    你得到了java.lang.ClassCastException,因为portfolios 是一个列表,而Publication 类中的portfolio(可能)不是,它可能是一个id(长);无法以任何有意义的方式将列表与 eq ('portfolio', portfolios) 中的 long 进行比较

    您不应该需要两个单独的查询,因为域类是相关的。

    --编辑-- 编辑不使用单独的操作,只使用列表操作。我也无法让include 工作,但下面是我在几十个案例中所拥有的。如果出于某种原因您不能这样做,那么关于仅使用 include 机制的新问题可能会引起一些关注。

    我不确定您当前的列表操作是什么样的。这就是我如何编写一个列表方法来获取所有投资组合及其最近的 5 个出版物。不需要任何参数,因为我要返回所有投资组合。

    //PortfolioController
    def list (){ 
        def portfolios = Portfolio.createCriteria().list {
            //if you needed to filter the list by for example portfolio status or something you could add that here
            or {
                eq('status','ACTIVE')
                eq('status','PENDING')
            }
            publications(org.hibernate.criterion.CriteriaSpecification.LEFT_JOIN) {
                eq("published", "Yes")
                order("lastUpdated", "desc")
                firstResult(5)
            }
        }
    
        [portfolios: portfolios, portfolioCount:portfolios.size()]
    }
    

    现在出版物已预先附加到他们的投资组合中。

    上述的 LEFT_JOIN 部分可确保您获得一份投资组合列表,其中仅附有符合条件的出版物;如果您忽略它,则默认为内部连接,并且当您迭代时,您将获得该投资组合的所有出版物(即使它们不符合条件)。

    然后在您的 gsp 中遍历投资组合 - 它可以直接在 list.gsp 中,也可以在 list.gsp 中呈现的模板中。如果你把它放在一个名为 _webList.gsp 的模板中,你会在 list.gsp 中渲染它

    <g:render template="weblist" model="['portfolios': portfolios]" />
    

    这在 list.gsp_webList.gsp 中 - 我会直接在 list.gsp 中开始使用它,以确保一切正常。

    <g:each in="${portfolios}" var="portfolioInstance" status="i">
        ${portfolioInstance?.portfolioName?.encodeAsHTML()
        <!-- whatever else you need here -->
        <!-- now iterate through the pubs -->
        <g:each in="${portfolioInstance.publications"} var="publicationInstance" status="j">
            ${publicationInstance.id}
            <!-- and again whatever else you need here -->
        </g:each>
    </g:each>
    

    --编辑-- firstResult(5) 似乎可以解决问题。 --编辑--

    您会注意到我在其中评论了 maxResults(5) - 我无法让它正常工作。即使它在关联块中,它似乎也控制了返回的投资组合的数量。也许其他人会看到这一点并添加那块拼图 - 或者自己修补它。如果我弄明白了,我会继续尝试和更新。

    【讨论】:

    • 谢谢伙计,这对我来说很有意义。来自红宝石世界,我遇到了标准问题。您在这短短的篇幅中已经解释了很多。对此,我真的非常感激。我稍后会尝试并告诉你结果。
    • 好的,我想我明白了。更新了使用firstResult(5) 的答案,在我的测试中,这似乎可以满足您的所有需求!让我知道这是否有效...
    • Kelly,好的,所以 sql 日志显示查询工作,但 gsp 迭代没有加载任何内容。我应该提到,视图代码位于投资组合目录“_weblist.gsp”中的部分模板中。我在portfolio/list.gsp 中有以下代码 奇怪的是它没有显示
    • 您可能需要将变量从主页传递到模板,例如:&lt;g:render template="/portfolio/weblist" model="['portfolios': portfolios]" /&gt;
    • 嗯还是一无所有。我必须躲开,但稍后会回来
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多