2012-11-29 35 views
0

編輯 -Grails的2.1個createCriteria問題使用參數

我試圖返回的投資組合列表,從我的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日誌。


EDIT

下面的代碼是從文件_webList.gsp整個部分。頁面頂部div -alert加載,但div屬性列表中的內容無法加載。使用Kelly的hibernate標準會在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> 

編輯 - 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 

回答

1

你所得到的java.lang.ClassCastException因爲portfolios是在Publication類(可能)的列表和portfolio不是,它可能是一個id(long);不能用任何有意義的方式來比較長列表eq ('portfolio', portfolios)

因爲域類是相關的,所以不應該需要兩個單獨的查詢。

- EDIT-- 編輯爲不使用單獨的動作,只使用列表動作。我還沒有能夠使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)似乎這樣的伎倆。 - EDIT--

你會注意到我有maxResults(5)評論在那裏 - 我很難讓它正常工作。它似乎控制即使在關聯區塊中也會返回的投資組合數量。也許別人會看到這一點,並添加這個難題 - 或自己修補它。如果我弄清楚,我會繼續嘗試和更新。

+0

謝謝隊友,這對我很有意義。來自紅寶石世界,我遇到了這個標準的問題。你在這個短的空間裏已經解釋了很多。對此,我真的非常感激。我會稍後嘗試,並讓你知道結果。 – IanN

+0

好吧,我想我明白了。更新了使用'firstResult(5)'的答案,並在我的測試中似乎做了你想做的一切!讓我知道這是否工作... – Kelly

+0

凱利,好,所以SQL日誌顯示查詢工作,但gsp迭代不加載任何東西。我應該提到視圖代碼位於投資組合目錄「_weblist.gsp」中的部分模板中。我在portfolio/list.gsp中有以下代碼奇怪,它不顯示 – IanN