2011-02-18 83 views
1

我有一個grails應用程序,它具有創建報告的服務。該報告定義爲:Grails服務不保存域對象當被Message Queue觸發時

class Report { 
    Date createDate 
    String reportType 
    List contents 

    static constraints = { 
    } 
} 

服務生成一份報告,並填充contents爲由createCriteria返回一個列表。

我的問題是,我的服務聲稱是保存報告,沒有錯誤出現,日誌記錄表明它的全部存在,但是當我去控制器上顯示該報告的顯示時,它說內容爲空。

另一個相關位,我的Service被一個ActiveMQ消息隊列調用。來自我的報告控制器的消息。

控制器:

class ReportController { 

    def scaffold = Report 

    def show = { 
     def rep = Report.get(params.id) 

     log.info("Report is " + (rep? "not null" : "null")) //says report is not null 
     log.info("Report content is " + (rep.contents? "not null" : "null")) //always says report.contents is null. 

     redirect(action: rep.reportType, model: [results: rep.contents, resultsTotal: rep.contents.size()]) 
    } 
} 

我的服務,創建報表:

class ReportService { 

static transactional = false 
static expose = ['jms'] 
static destination = "Report" 

void onMessage(msg) 
{ 
    this."$msg.reportType"(msg) 
} 

void totalQuery(msg) 
{  
    def results = Result.createCriteria().list { 
     //This returns exactly what i need. 
    } 

    Report.withTransaction() { 
     def rep = new Report(createDate: new Date(), reportType: "totalQuery", contents: results) 

     log.info("Validation results: ${rep.validate()}") 

     if(!rep.save(flush: true)) { 
      rep.errors.each { 
       log.error(it) 
      } 
     } 
    } 
} 

有沒有辦法,我在這裏失蹤了一些東西明顯?我的想法是,因爲所有的單元測試工作,hibernate上下文沒有通過消息隊列傳遞。但那會產生例外,不是嗎?我一直在這個問題上打了幾天頭,所以在正確的方向上的一個點會很棒。

感謝,

+0

如何找到保存的報告的ID?我沒有看到任何可以通過它的代碼或記錄它。 `list`操作是否顯示新創建的報告? – 2011-02-18 15:27:12

+0

應用程序使用自動生成的列表視圖。點擊身份證號碼「show」 – 2011-02-18 15:34:57

回答

3

不能定義任意List喜歡,所以它越來越忽略和瞬態處理。如果你有一個def name字段,你會得到相同的行爲,因爲在這兩種情況下,Hibernate不知道數據類型,所以它不知道如何將它映射到數據庫。

如果你想指結果的集合,那麼你需要一個hasMany

class Report { 
    Date createDate 
    String reportType 

    static hasMany = [contents: Result] 
} 

如果需要排序列表,然後還加在List場使用相同的名稱,而不是創建一個Set(默認值),這將是一個List

class Report { 
    Date createDate 
    String reportType 

    List contents 
    static hasMany = [contents: Result] 
} 

你的單元測試工作,因爲你沒有訪問數據庫或使用Hibernate。我認爲最好始終集成測試域類,以便至少使用內存數據庫,並在測試控制器,服務等時模擬域類。

相關問題